问:
Moving websites from dedicated server to cloud server
We have few website hosted on a dedicated server and in case we decide to move website from dedicated server to cloud server then what should we consider from programing point of view. All our website are Asp.Net based & use different session variable and rely on MS SQL Server standard 2012 database.

there is lot of information available on internet some of which is confusing and even confusion is type of cloud environment. Service provided sale team always push to move for cloud But when we ask these question then there is no clear answer.

I faced one such issue with shared hosting few year back when one of the web websites kept loosing session variable for same request by same user after investigating i found that shared hosting was farm based shared hosting so same user session was served by different servers in the farm which result in session variable loss in case first request went to server 1 & second request went to server 2. This issue was resolved by adding machine key in web.config.

and i have no idea honestly how cloud servers work. I am worried about session variable loss which i can get around by using SQL based session state.

What are the other precision that one should take before signing up for cloud based web server, from configuration or programing point of view.

答:
There are few points that you need to understand while working on cloud server.

If we are using a single server such as ECS by Alibaba cloud or any other cloud provider, then the set up would be same as that we have with physical server.

If we are using managed service such as RDS by Alibaba cloud, then network string (db url ) needs to taken care of.

If we are using multiple servers behind load balancer on cloud such as SLB on Alicloud cloud then session maintenance has to be taken care off. It is recommended to use centralized session manager such as Memcache. Otherwise we would need to tweak the loadbalancer configuration.

Most of my references are from Alicloud as I have worked on it so bear with me. You can also check their web hosting packages


来源: stackexchange

问:
Limit Apache Camel RabbitMQ number of connections
Is there any way to limit the number of connections that the Apache Camel RabbitMQ systems makes to the RabbitMQ server.  By default it seems to create a connection for each Camel consumer/producer.</p>

答: You can check options "threadPoolSize" and "channelPoolMaxSize" (http://camel.apache.org/rabbitmq.html). Looks like the second one is what you need.</p>

ContentLicense: CC BY-SA 3.0


问:
How to create word docs programmatically from a template
I am trying to create about 600 reports in Microsoft office Word. The documents are populated with data from a database, and images found on a local drive.
I have figured out, that I might create a Word Template project in visual studio 2010, and program the template, so that when you enter a single value (id-number), it automatically fills out the entire document.

I am quite confident that this is possible. the only problem is. How do I loop through all entries in the database, open a new document based on the template and set the id-value?

```<code>for(int i = 0; i < idnumbers.Count(); i++)
{
     Word.Application app = new Word.Application();
     Word.Document doc = app.Documents.Add(@"C:\..\WordGenerator\bin\Debug\WordTemplate.dotx");
     //input the id-number below: HOW??

     doc.SaveAs(FileName: @"c:\temp\test.docx");
}
</code>```

The application is supposed to run only once, generating the reports, and it doesn´t have to be fast. It just has to be easy to develop.

The problem here is, that it seems that the DocumentBase object is not accessible outside the Word project. The substitute Microsoft.Office.Interop.Word.Document does not have functionality like SelectContentControlsByTitle that allows me to find and set my ContentControls. And that is exactly what I need to do..

EDIT: This is what my code looks like now to insert the text into my field:

```<code>Word.Application app = new Word.Application();

Word.Document doc = app.Documents.Add(@"C:\..\test.dotx");

foreach (Word.ContentControl cc in doc.SelectContentControlsByTitle("MyCCTitle"))
{
    cc.Range.Text += "1234";
}

doc.SaveAs(FileName: @"c:\temp\test.docx");
</code>```

Then an eventhandler on my template on BeforeSave fills out the document based on the text in MyCCTitle-titled object.

答:
Seems that there are 2 questions here:

How do you kick off the process for a particular id-value

How do you populate the document.

sunilp has answered Q2. Data bound content controls are the best way to inject data for Word 2007 and later.

OP's focus looks to be Q1.

There is no command line switch that lets you pass an arbitrary value to Word: http://support.microsoft.com/kb/210565

So as I see it you have 4 choices:

do all the work via the OpenXML SDK, never opening Word at all (as other posters have suggested)

create a minimal pre-existing document (containing your id number) using the OpenXML SDk, then open Word

automate Word to pass the id number to the document, perhaps as a document property

do the work to create the 600 documents in Word using VSTO or Word macros (VBA)

Me? I would create a docx containing data bound content controls in Word, and save it.

Then, in would inject my data into it as a custom xml part, and save it.  (This step you could do using the OpenXML SDK, or in Word if you needed to have Word update the bindings for some downstream process of yours)

ContentLicense: CC BY-SA 4.0

Body:
I am trying to create about 600 reports in Microsoft office Word. The documents are populated with data from a database, and images found on a local drive.
I have figured out, that I might create a Word Template project in visual studio 2010, and program the template, so that when you enter a single value (id-number), it automatically fills out the entire document.

I am quite confident that this is possible. the only problem is. How do I loop through all entries in the database, open a new document based on the template and set the id-value?

for(int i = 0; i < idnumbers.Count(); i++)
{
     Word.Application app = new Word.Application();
     Word.Document doc = app.Documents.Add(@"C:\..\WordGenerator\bin\Debug\WordTemplate.dotx");
     //input the id-number below: HOW??

     doc.SaveAs(FileName: @"c:\temp\test.docx");
}

The application is supposed to run only once, generating the reports, and it doesn´t have to be fast. It just has to be easy to develop.

The problem here is, that it seems that the DocumentBase object is not accessible outside the Word project. The substitute Microsoft.Office.Interop.Word.Document does not have functionality like SelectContentControlsByTitle that allows me to find and set my ContentControls. And that is exactly what I need to do..

EDIT: This is what my code looks like now to insert the text into my field:

Word.Application app = new Word.Application();

Word.Document doc = app.Documents.Add(@"C:\..\test.dotx");

foreach (Word.ContentControl cc in doc.SelectContentControlsByTitle("MyCCTitle"))
{
    cc.Range.Text += "1234";
}

doc.SaveAs(FileName: @"c:\temp\test.docx");

Then an eventhandler on my template on BeforeSave fills out the document based on the text in MyCCTitle-titled object.

ContentLicense: CC BY-SA 2.5


问:
Add new child entity and update parent entity in one turn
I have table entry and a table type. An entry has a type a type can be in many entries (n-to-1 relation). The type is displayed in an editable ComboBox. If the user enters a type which is not yet existing it should be created and the type of the related entry should be updated to the new type-entity.

How I load the entries (eagerly loading):

```<code>var entries = dbContext.entry.Include(entry => entry.type).ToList();
</code>```

How I bind the type to the Combobox:

```<code><Combobox IsEditable="True"
    ItemsSource="{Binding AllTypes, Source={StaticResource typeTableController}}"
    SelectedValue="{Binding Path="Entry.type"}" />
</code>```

typeTableController has a property (AllTypes) with all existing types in the DB. Entry is the currently selected entry. The user can now edit the ComboBox text. If he presses a "Save" button this type should be added to the DB if not existing yet, or it should be selected if existing. And the updated entry should be saved. It would be great if I don't have to handle every property on its own because type is not the only one I have.

My first approaches:

```<code>dbContext.Set(entry.getType()).Attach(entry);
dbContext.SaveChanges();
</code>```

-> InvalidOperationException: referential integrity constraint violation

or

```<code>dbContext.Entry(formEntry.type).State = EntityState.Added;
dbContext.Set(entry.getType()).Add(entry);
dbContext.SaveChanges();
</code>```

-> Adds the new type to the type table BUT also duplicates all types and entries in the type/entry table (that is really strange for me) and only updates the type of one of the now two existing entries to the new type. So if there were 10 types before, there are now 21.

Edit: my entry:

```<code>public partial class entry
{
    ...
    public Nullable<int> type_id { get; set; }
    ...
    public virtual type type { get; set; }
}
</code>```

Important:
I always got crashes when I try

```<code>dbContext.Entry(entry).State = EntityState.Modified;
</code>```

The reason was, that I had entry in my debugger watchlist. It seems that this leads to some conflicts. Removing it fixes that problem.

答:
Determine if type is existing one or new added by user. Check type.Id == 0 for new type entity.

```<code>if (typeFromCombobox.Id == 0){
    //new type added
    dbContext.Set<type>().Add(typeFromCombobox)
}
entity.type = typeFromCombobox // EF should handle this if you didn't call 'Detach' before
dbContext.SaveChanges()
</code>```

Edit: not sure about that: you are using combobox binding which should automatically change entity.type when user select it. So line entity.type = typeFromCombobox is rebundant

Edit 2: I created sample code

It works fine and both entity.typeid and entity.type.id were updated after SaveChanges call

But if I uncomment this.Configuration.AutoDetectChangesEnabled = false; entity.typeid won't be updated until I call context.ChangeTracker.DetectChanges(); manually

Edit 3: If you changed dbContext between load and save you should attach you entity to new context

```<code>dbContext.Set<entity>().Attach(entity);
if (typeFromCombobox.Id == 0){
    //new type added
    dbContext.Set<type>().Add(typeFromCombobox)
}
entity.type = typeFromCombobox;
dbContext.SaveChanges()
</code>```

Body:
I have table entry and a table type. An entry has a type a type can be in many entries (n-to-1 relation). The type is displayed in an editable ComboBox. If the user enters a type which is not yet existing it should be created and the type of the related entry should be updated to the new type-entity.

How I load the entries (eagerly loading):

var entries = dbContext.entry.Include(entry => entry.type).ToList();

How I bind the type to the Combobox:

<Combobox IsEditable="True"
    ItemsSource="{Binding AllTypes, Source={StaticResource typeTableController}}"
    SelectedValue="{Binding Path="Entry.type"}" />

typeTableController has a property (AllTypes) with all existing types in the DB. Entry is the currently selected entry. The user can now edit the ComboBox text. If he presses a "Save" button this type should be added to the DB if not existing yet, or it should be selected if existing. And the updated entry should be saved. It would be great if I don't have to handle every property on its own because type is not the only one I have.

My first approaches:

dbContext.Set(entry.getType()).Attach(entry);
dbContext.SaveChanges();

-> InvalidOperationException: referential integrity constraint violation

or

dbContext.Entry(formEntry.type).State = EntityState.Added;
dbContext.Set(entry.getType()).Add(entry);
dbContext.SaveChanges();

-> Adds the new type to the type table BUT also duplicates all types and entries in the type/entry table (that is really strange for me) and only updates the type of one of the now two existing entries to the new type. So if there were 10 types before, there are now 21.

Edit: my entry:

public partial class entry
{
    ...
    public Nullable<int> type_id { get; set; }
    ...
    public virtual type type { get; set; }
}

Important:
I always got crashes when I try

dbContext.Entry(entry).State = EntityState.Modified;

The reason was, that I had entry in my debugger watchlist. It seems that this leads to some conflicts. Removing it fixes that problem.

Body:
Determine if type is existing one or new added by user. Check type.Id == 0 for new type entity.

if (typeFromCombobox.Id == 0){
    //new type added
    dbContext.Set<type>().Add(typeFromCombobox)
}
entity.type = typeFromCombobox // EF should handle this if you didn't call 'Detach' before
dbContext.SaveChanges()

Edit: not sure about that: you are using combobox binding which should automatically change entity.type when user select it. So line entity.type = typeFromCombobox is rebundant

Edit 2: I created sample code

It works fine and both entity.typeid and entity.type.id were updated after SaveChanges call

But if I uncomment this.Configuration.AutoDetectChangesEnabled = false; entity.typeid won't be updated until I call context.ChangeTracker.DetectChanges(); manually

Edit 3: If you changed dbContext between load and save you should attach you entity to new context

dbContext.Set<entity>().Attach(entity);
if (typeFromCombobox.Id == 0){
    //new type added
    dbContext.Set<type>().Add(typeFromCombobox)
}
entity.type = typeFromCombobox;
dbContext.SaveChanges()


问:
Disable pagination in Yii2 gridview
How to disable pagination in Yii2 GridView? I am not using any kartik gridview. I am using the default Yii2 Gridview.</p>

答:
1) If you have a limited number of models, use ArrayDataProvider and get all models, instead of ActiveDataProvider.

2) You can specify it in the view, the layout property of the GridView can be set to {summary}{items}. The default one includes {pager} too.

Title: Disable pagination in Yii2 gridview


问:
Open PDF in a WebView
I want to open a PDF in my WebView, and I found and combined codes on this forum.

But it catches the "No PDF application found" although I have multiple PDF apps installed, including Adobe Reader.

Here the code:

```<code>private class PsvWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            if (url.contains(".pdf")) {
                Uri path = Uri.parse(url);
                Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                pdfIntent.setDataAndType(path, "application/pdf");
                pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try
                {
                    startActivity(pdfIntent);
                }
                catch(ActivityNotFoundException e)
                {
                    Toast.makeText(PsvWebViewActivity.this, "No PDF application found", Toast.LENGTH_SHORT).show();
                }
                catch(Exception otherException)
                {
                    Toast.makeText(PsvWebViewActivity.this, "Unknown error", Toast.LENGTH_SHORT).show();
                }

            }

            return true;
        }   } }
</code>```

答:
I know, this question is old.

But I really like the approach of Xamarin to make use of the pdf.js from Mozilla. It works on older Android versions, you don't need a special PDF Viewer app for this and you can easily display a PDF inside of your apps views hierarchy.

Git for this: https://mozilla.github.io/pdf.js/

Additional options: https://github.com/mozilla/pdf.js/wiki/Viewer-options

Just add the pdfjs files to your Assets directory:

And call it the following way:

```<code>// Assuming you got your pdf file:
File file = new File(Environment.getExternalStorageDirectory() + "/test.pdf");

webview = (WebView) findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + file.getAbsolutePath());
</code>```

Cool thing: If you want to reduce the amount of functionalities / controls. Go to the Assets/pdfjs/web/viewer.html file and mark certain controls as hidden. With

```<code>style="display: none;"
</code>```

E.g. If you don't like the right toolbar:

```<code><div id="toolbarViewerRight" style="display: none;">...</div>
</code>```

Title: Open PDF in a WebView

Body:
I want to open a PDF in my WebView, and I found and combined codes on this forum.

But it catches the "No PDF application found" although I have multiple PDF apps installed, including Adobe Reader.

Here the code:

private class PsvWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            if (url.contains(".pdf")) {
                Uri path = Uri.parse(url);
                Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                pdfIntent.setDataAndType(path, "application/pdf");
                pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try
                {
                    startActivity(pdfIntent);
                }
                catch(ActivityNotFoundException e)
                {
                    Toast.makeText(PsvWebViewActivity.this, "No PDF application found", Toast.LENGTH_SHORT).show();
                }
                catch(Exception otherException)
                {
                    Toast.makeText(PsvWebViewActivity.this, "Unknown error", Toast.LENGTH_SHORT).show();
                }

            }

            return true;
        }   } }

Body:
I know, this question is old.

But I really like the approach of Xamarin to make use of the pdf.js from Mozilla. It works on older Android versions, you don't need a special PDF Viewer app for this and you can easily display a PDF inside of your apps views hierarchy.

Git for this: https://mozilla.github.io/pdf.js/

Additional options: https://github.com/mozilla/pdf.js/wiki/Viewer-options

Just add the pdfjs files to your Assets directory:

And call it the following way:

// Assuming you got your pdf file:
File file = new File(Environment.getExternalStorageDirectory() + "/test.pdf");

webview = (WebView) findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + file.getAbsolutePath());

Cool thing: If you want to reduce the amount of functionalities / controls. Go to the Assets/pdfjs/web/viewer.html file and mark certain controls as hidden. With

style="display: none;"

E.g. If you don't like the right toolbar:

<div id="toolbarViewerRight" style="display: none;">...</div>


问:
ggplot2: how to sort the categories in horizontal bar charts?
I have a hard time finding the correct way to reoder my text x axis in ggplot.
Consider this simple example:

```<code>library(ggplot2)
library(dplyr)
dataframe <- data_frame('group' = c(1,1,1,2,2,2),
                        'text' = c('hello', 'world', 'nice', 'hello', 'magic', 'bug'),
                        'count' = c(12,10,3,4,3,2))

> dataframe
# A tibble: 6 × 3
  group  text count
  <dbl> <chr> <dbl>
1     1 hello    12
2     1 world    10
3     1  nice     3
4     2 hello     4
5     2 magic     3
6     2   bug     2
</code>```

and now the chart

```<code>ggplot(dataframe, aes(x = text, y = count, fill = count, group = group)) +
  geom_bar(stat = 'identity') +
  facet_wrap(~ group,  scales = "free_y") +
  coord_flip()
</code>```

Problem is: I would like to sort the words in increasing count order, so that the word with the highest count appears on the bottom for each category.

Using the solutions in Order Bars in ggplot2 bar graph and ggplot bar plot with facet-dependent order of categories does not help.

I suspect this is a problem related to the horizontal alilgnment. For instance, using

```<code>ggplot(dataframe, aes(x = reorder(text, -count), y = count, fill = count, group = group)) +
  geom_bar(stat = 'identity') +
  facet_wrap(~ group,  scales = "free_y") +
  coord_flip()
</code>```

only sorts one chart (on the right).

Any ideas?
Thanks!

```<code>> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] dplyr_0.5.0   ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.9      digest_0.6.12    assertthat_0.1   grid_3.3.2       plyr_1.8.4
 [6] R6_2.2.0         gtable_0.2.0     DBI_0.5-1        magrittr_1.5     scales_0.4.1
[11] lazyeval_0.2.0   labeling_0.3     tools_3.3.2      munsell_0.4.3    colorspace_1.3-2
[16] tibble_1.2
</code>```

答:
I removed some useless parts like the group, used 'modernized' geom_col(), but the trick was probably in doing sum per factor level instead of mean, which is the default for reorder. Consistently using the tidyverse functions usually saves you from unpleasant surprises, even if reorder would work here as well.

```<code>library(tidyverse)

dataframe %>%
  mutate(text = text %>% forcats::fct_reorder(count, sum)) %>%
  ggplot(aes(x = text, y = count, fill = count)) +
  geom_col() +
  facet_wrap(~ group,  scales = "free_y") +
  coord_flip()
</code>```

Keep in mind that there is only one ordering of the factor, which means that in the two facets you can have opposite sorting, if you craft your data accordingly (ie there is no sort per facet afaik).

Body:
I have a hard time finding the correct way to reoder my text x axis in ggplot.
Consider this simple example:

library(ggplot2)
library(dplyr)
dataframe <- data_frame('group' = c(1,1,1,2,2,2),
                        'text' = c('hello', 'world', 'nice', 'hello', 'magic', 'bug'),
                        'count' = c(12,10,3,4,3,2))

> dataframe
# A tibble: 6 × 3
  group  text count
  <dbl> <chr> <dbl>
1     1 hello    12
2     1 world    10
3     1  nice     3
4     2 hello     4
5     2 magic     3
6     2   bug     2

and now the chart

ggplot(dataframe, aes(x = text, y = count, fill = count, group = group)) +
  geom_bar(stat = 'identity') +
  facet_wrap(~ group,  scales = "free_y") +
  coord_flip()

Problem is: I would like to sort the words in increasing count order, so that the word with the highest count appears on the bottom for each category.

Using the solutions in Order Bars in ggplot2 bar graph and ggplot bar plot with facet-dependent order of categories does not help.

I suspect this is a problem related to the horizontal alilgnment. For instance, using

ggplot(dataframe, aes(x = reorder(text, -count), y = count, fill = count, group = group)) +
  geom_bar(stat = 'identity') +
  facet_wrap(~ group,  scales = "free_y") +
  coord_flip()

only sorts one chart (on the right).

Any ideas?
Thanks!

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] dplyr_0.5.0   ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.9      digest_0.6.12    assertthat_0.1   grid_3.3.2       plyr_1.8.4
 [6] R6_2.2.0         gtable_0.2.0     DBI_0.5-1        magrittr_1.5     scales_0.4.1
[11] lazyeval_0.2.0   labeling_0.3     tools_3.3.2      munsell_0.4.3    colorspace_1.3-2
[16] tibble_1.2

Body:
I removed some useless parts like the group, used 'modernized' geom_col(), but the trick was probably in doing sum per factor level instead of mean, which is the default for reorder. Consistently using the tidyverse functions usually saves you from unpleasant surprises, even if reorder would work here as well.

library(tidyverse)

dataframe %>%
  mutate(text = text %>% forcats::fct_reorder(count, sum)) %>%
  ggplot(aes(x = text, y = count, fill = count)) +
  geom_col() +
  facet_wrap(~ group,  scales = "free_y") +
  coord_flip()

Keep in mind that there is only one ordering of the factor, which means that in the two facets you can have opposite sorting, if you craft your data accordingly (ie there is no sort per facet afaik).


问:
How to count word frequency from a Pandas Dataframe- Python
I currently created a Pandas Dataframe from a dictionary.
The Dataframe looks something like:

```<code>      URL         TITLE
0   /xxxx.xx   Hi this is word count
1   /xxxx.xx   Hi this is Stack Overflow
2   /xxxx.xx   Stack Overflow Questions
</code>```

I want to add a new column to this table, which lists the number of frequency the word "Stack Overflow" appears. So for example, it would be like:

```<code>      URL         TITLE                          COUNT
0   /xxxx.xx   Hi this is word count               0
1   /xxxx.xx   Hi this is Stack Overflow           1
2   /xxxx.xx   Stack Overflow Questions            1
</code>```

The count function does not seem to work for dictionaries, but only for strings.  Would there bean easy way to do this?

答:
Assuming this is actually a pandas dataframe, you could do:

```<code>import pandas as pd

table = {   'URL': ['/xxxx.xx', '/xxxx.xx', '/xxxx.xx'],
            'TITLE': ['Hi this is word count', 'Hi this is Stack Overflow', 'Stack Overflow Questions']}

df = pd.DataFrame(table)
df['COUNT'] = df.TITLE.str.count('Stack Overflow')
print(df)
</code>```

This yields:

```<code>                       TITLE       URL  COUNT
0      Hi this is word count  /xxxx.xx      0
1  Hi this is Stack Overflow  /xxxx.xx      1
2   Stack Overflow Questions  /xxxx.xx      1
</code>```

Body:
I currently created a Pandas Dataframe from a dictionary.
The Dataframe looks something like:

      URL         TITLE
0   /xxxx.xx   Hi this is word count
1   /xxxx.xx   Hi this is Stack Overflow
2   /xxxx.xx   Stack Overflow Questions

I want to add a new column to this table, which lists the number of frequency the word "Stack Overflow" appears. So for example, it would be like:

      URL         TITLE                          COUNT
0   /xxxx.xx   Hi this is word count               0
1   /xxxx.xx   Hi this is Stack Overflow           1
2   /xxxx.xx   Stack Overflow Questions            1

The count function does not seem to work for dictionaries, but only for strings.  Would there bean easy way to do this?

Body:
Assuming this is actually a pandas dataframe, you could do:

import pandas as pd

table = {   'URL': ['/xxxx.xx', '/xxxx.xx', '/xxxx.xx'],
            'TITLE': ['Hi this is word count', 'Hi this is Stack Overflow', 'Stack Overflow Questions']}

df = pd.DataFrame(table)
df['COUNT'] = df.TITLE.str.count('Stack Overflow')
print(df)

This yields:

                       TITLE       URL  COUNT
0      Hi this is word count  /xxxx.xx      0
1  Hi this is Stack Overflow  /xxxx.xx      1
2   Stack Overflow Questions  /xxxx.xx      1


问:
wp-content directory files into an archive file named files.tar.gz
I am trying  to put wp-content directory into an archive file named files.tar.gz. I am doing this through putty on the external sever where wordpress is placed.

After command:

```<code>tar -zcvf files.tar.gz /public_html/denim4u/wp-content
</code>```

I receive:

tar: Removing leading `/' from member names

  tar:
  /public_html/denim4u/wp-content: Cannot stat: No such file or
  directory

  tar: Exiting with failure status due to previous errors

What am I doing wrong?

答:
According to this error message :

tar: Removing leading `/' from member names

  tar: /public_html/denim4u/wp-content: Cannot stat: No such file or directory

tar: Exiting with failure status due to previous errors

The directory you want to compress is not a valid path.

Best regards

Body:
I am trying  to put wp-content directory into an archive file named files.tar.gz. I am doing this through putty on the external sever where wordpress is placed.

After command:

tar -zcvf files.tar.gz /public_html/denim4u/wp-content

I receive:

tar: Removing leading `/' from member names

  tar:
  /public_html/denim4u/wp-content: Cannot stat: No such file or
  directory

  tar: Exiting with failure status due to previous errors

What am I doing wrong?


问:
angular.toJSON() function/method implementation on node.js
Is it possible to somehow implement angular.toJSON function/method on node.js server. All I need is to use angular.toJSON().

I got to it due to hash-key error with '$' when writing data to mongo database.

Or maybe there is a function that would replace angular.toJSON...

Thanks!

EDIT:

Here is the full explanation of the problem and the solution:

Angular gives $scope objects in ng-repeat a "&&hashKey" so it can track any changes and if change occurs then it updates DOM. When You send the object to the node server it still has hashKey elemnt inside itself and it has no effect to most of things in node. However when saving $$hashKey to mongo database via mongoose it throws an error that the key of and element in object should not start with '$'.

So all we need is to remove the hash key. There is an angular.toJSON() method that automatically removed $$hashKey from object but it ONLY WORKS ON CLIENT SIDE.

To implement something that would work the same way on node.js ste2425 proposed using JSON.strigify with replacer option. In final I made a function that can be implemented on node.js that works for my problem (do not know if it is a best solution):

```<code>function angularToJSON(object) {
    replacer = (key, val) => key.startsWith('$$') ? undefined : val;
    var objectReplaced = JSON.stringify(object, replacer);
    return JSON.parse(objectReplaced);
}
</code>```

答:
This doesn't replicate angulars toJSON as i'm not entirely sure of its inner workings.

However if you want to filter/sanitize an object whilst is being serialized you can use a replacer method available to the standard JSON.stringify

```<code>const obj = {
    one: 10,
    $$two: 'ignore',
    three: {
      $$four: 'troll',
      five: 50
    },
    array: [{
        $$tt: 10,
        bob: 5
      },
      5
    ]
  },
  replacer = (key, val) => key.startsWith('$$') ? undefined : val;

console.log(JSON.stringify(obj, replacer));

// {"one":10,"three":{"five":50},"array":[{"bob":5},5]}
</code>```

see fiddle

Note I'm sure ive read that key could be undefined in some circumstances but couldn't find it on the MDN or replicate it in the fiddle, that could be for de-serializing. Just FYI really.

Body:
Is it possible to somehow implement angular.toJSON function/method on node.js server. All I need is to use angular.toJSON().

I got to it due to hash-key error with '$' when writing data to mongo database.

Or maybe there is a function that would replace angular.toJSON...

Thanks!

EDIT:

Here is the full explanation of the problem and the solution:

Angular gives $scope objects in ng-repeat a "&&hashKey" so it can track any changes and if change occurs then it updates DOM. When You send the object to the node server it still has hashKey elemnt inside itself and it has no effect to most of things in node. However when saving $$hashKey to mongo database via mongoose it throws an error that the key of and element in object should not start with '$'.

So all we need is to remove the hash key. There is an angular.toJSON() method that automatically removed $$hashKey from object but it ONLY WORKS ON CLIENT SIDE.

To implement something that would work the same way on node.js ste2425 proposed using JSON.strigify with replacer option. In final I made a function that can be implemented on node.js that works for my problem (do not know if it is a best solution):

function angularToJSON(object) {
    replacer = (key, val) => key.startsWith('$$') ? undefined : val;
    var objectReplaced = JSON.stringify(object, replacer);
    return JSON.parse(objectReplaced);
}

Body:
This doesn't replicate angulars toJSON as i'm not entirely sure of its inner workings.

However if you want to filter/sanitize an object whilst is being serialized you can use a replacer method available to the standard JSON.stringify

const obj = {
    one: 10,
    $$two: 'ignore',
    three: {
      $$four: 'troll',
      five: 50
    },
    array: [{
        $$tt: 10,
        bob: 5
      },
      5
    ]
  },
  replacer = (key, val) => key.startsWith('$$') ? undefined : val;

console.log(JSON.stringify(obj, replacer));

// {"one":10,"three":{"five":50},"array":[{"bob":5},5]}

see fiddle

Note I'm sure ive read that key could be undefined in some circumstances but couldn't find it on the MDN or replicate it in the fiddle, that could be for de-serializing. Just FYI really.


问:
How can I fix "path for project must have only one segment" error when creating a new GWT project in eclipse?
I'm using Eclipse Helios on Windows 7, with GWT SDK 2.3.  I've got a few GWT projects with that same GWT SDK. Those projects were created a couple years ago. I'm trying to create a new 'Web Application Project' (aka GWT or GAE project) and when I do so, I get the following error:

Creation of element failed.

Path for project must have only one segment.

I entered the project name and package and unchecked the GAE box. I suspect the problem lies with spaces in the GWT path, because after the 'Use default SDK' radio button it has '(gwt-2.3.0 - 2.3.0)'. If I click on 'Configure SDKs' in the GWT section, the grid inside the dialog there for the GWT SDKs has columns for Name, Version, and Location, and it looks like it is creating that (gwt-2.3.0 - 2.3.0) from the Name and Version.

I've updated the Google plugin to the latest version. I've downloaded GWT SDK 2.5 and tried using that SDK to create a project. I tried a new 'Web Application Project' with GWT unchecked and GAE checked.  All result in the same problem.

Any suggestions?

答:
I had the same problem
turned out that in my pom.xml file i did not specified artifact, see below:

```<code><groupId>com.example</groupId>
<artifactId></artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
</code>```

In this case eclipse read the project as:

com.example: :0.0.1-SNAPSHOT

However if you specify the artifact like below

```<code><groupId>com.example</groupId>
<artifactId>abc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
</code>```

Eclipce will import it as

com.example:org:0.0.1-SNAPSHOT

Body:
I had the same problem
turned out that in my pom.xml file i did not specified artifact, see below:

<groupId>com.example</groupId>
<artifactId></artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

In this case eclipse read the project as:

com.example: :0.0.1-SNAPSHOT

However if you specify the artifact like below

<groupId>com.example</groupId>
<artifactId>abc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

Eclipce will import it as

com.example:org:0.0.1-SNAPSHOT


问:
The Content-Security-Policy directive 'child-src' is implemented behind a flag which is currently disabled
The Content-Security-Policy directive 'child-src' is implemented behind a flag which is currently disabled.

This the error showing when I am trying to build a cordova app .

I am using the meta for  overcoming  it

```<code><meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-eval' data: blob: filesystem: ws: gap: cdvfile: https://ssl.gstatic.com *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; img-src * data: 'unsafe-inline'; connect-src * 'unsafe-inline'; child-src *;" />
</code>```

答:
It's coming from here and because the name of the method is reportUnsupportedDirective() my guess would be that the directive is parsed as such but as the error indicates some flag must be switched to really activate it.

What I think is the most likely case: it is detected but not implemented yet. At least this method over here does not mention my particular case that I face right now: worker-src.

The only call I found so far is from this piece of code.

Everything above reverse-engineered, so take it with a grain of salt.

Body:
The Content-Security-Policy directive 'child-src' is implemented behind a flag which is currently disabled.

This the error showing when I am trying to build a cordova app .

I am using the meta for  overcoming  it

<meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-eval' data: blob: filesystem: ws: gap: cdvfile: https://ssl.gstatic.com *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; img-src * data: 'unsafe-inline'; connect-src * 'unsafe-inline'; child-src *;" />


问:
Find elements on webpage with Selenium
I have XPath

```<code>/html/body/div[@id='page']/div[@id='page-inner']/div[@id='main-box']/div[@class='in1']/div[@id='content-and-context']/div[@id='content']/div[@class='under-bar']/table[@class='flights']/tbody/tr[@id='flight-932539']/td[2]:
</code>```

But flight-number are changes. Can I find Elements with part XPath ?
I use foreach() and write data for every flight.

this is html code:

答:
As Guy mentioned xpath above, for same you can easily use findElements to find all the flight details and then according perform you actions using for loop.

```<code>List<WebElement> WebElements = driver.findElements(By.xpath("//tr[contains(@id, 'flight-')]/td[2]");

for(WebElement element : WebElements){
     //perform any operation like for click you can use
     element.getText();
}
</code>```

Above example is in JAVA you can do same in C# as well.

Title: Find elements on webpage with Selenium

Body:
I have XPath

/html/body/div[@id='page']/div[@id='page-inner']/div[@id='main-box']/div[@class='in1']/div[@id='content-and-context']/div[@id='content']/div[@class='under-bar']/table[@class='flights']/tbody/tr[@id='flight-932539']/td[2]:

But flight-number are changes. Can I find Elements with part XPath ?
I use foreach() and write data for every flight.

this is html code:

Body:
As Guy mentioned xpath above, for same you can easily use findElements to find all the flight details and then according perform you actions using for loop.

List<WebElement> WebElements = driver.findElements(By.xpath("//tr[contains(@id, 'flight-')]/td[2]");

for(WebElement element : WebElements){
     //perform any operation like for click you can use
     element.getText();
}

Above example is in JAVA you can do same in C# as well.


问:
How do I unit test a Servlet Filter with jUnit?
Implemented doFilter(). How to properly cover Filter with jUnit ?

```<code>public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws java.io.IOException, javax.servlet.ServletException
{
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    String currentURL = request.getRequestURI();

    if (!currentURL.equals("/maintenance.jsp") && modeService.getOnline())
    {
        response.sendRedirect("/maintenance.jsp");
    }
    filterChain.doFilter(servletRequest, servletResponse);
}
</code>```

答:
If you are using Spring then it has own mocks:

org.springframework.mock.web.MockFilterChain

org.springframework.mock.web.MockHttpServletRequest

org.springframework.mock.web.MockHttpServletResponse

For example, you can add headers and set test Uri.

So your test can be smth like this:

```<code>@RunWith(MockitoJUnitRunner.class)
public class TokenAuthenticationFilterTest {

    private static final String token = "260bce87-6be9-4897-add7-b3b675952538";
    private static final String testUri = "/testUri";

    @Mock
    private SecurityService securityService;

    @InjectMocks
    private TokenAuthenticationFilter tokenAuthenticationFilter = new TokenAuthenticationFilter();

    @Test
    public void testDoFilterInternalPositiveScenarioWhenTokenIsInHeader() throws ServletException, IOException {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.addHeader(TOKEN, token);
        request.setRequestURI(testUri);
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain filterChain = new MockFilterChain();
        when(securityService.doesExistToken(token)).thenReturn(true);
        tokenAuthenticationFilter.doFilterInternal(request, response, filterChain);
        assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
    }
}
</code>```

Body:
Implemented doFilter(). How to properly cover Filter with jUnit ?

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws java.io.IOException, javax.servlet.ServletException
{
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    String currentURL = request.getRequestURI();

    if (!currentURL.equals("/maintenance.jsp") && modeService.getOnline())
    {
        response.sendRedirect("/maintenance.jsp");
    }
    filterChain.doFilter(servletRequest, servletResponse);
}

Body:
If you are using Spring then it has own mocks:

org.springframework.mock.web.MockFilterChain

org.springframework.mock.web.MockHttpServletRequest

org.springframework.mock.web.MockHttpServletResponse

For example, you can add headers and set test Uri.

So your test can be smth like this:

@RunWith(MockitoJUnitRunner.class)
public class TokenAuthenticationFilterTest {

    private static final String token = "260bce87-6be9-4897-add7-b3b675952538";
    private static final String testUri = "/testUri";

    @Mock
    private SecurityService securityService;

    @InjectMocks
    private TokenAuthenticationFilter tokenAuthenticationFilter = new TokenAuthenticationFilter();

    @Test
    public void testDoFilterInternalPositiveScenarioWhenTokenIsInHeader() throws ServletException, IOException {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.addHeader(TOKEN, token);
        request.setRequestURI(testUri);
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockFilterChain filterChain = new MockFilterChain();
        when(securityService.doesExistToken(token)).thenReturn(true);
        tokenAuthenticationFilter.doFilterInternal(request, response, filterChain);
        assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
    }
}


问:
ASP.NET C# json data from string using regex
I want get Below picture json data sender>id:"value" and message>text: "value"

just get value string

use asp.net c# Text.RegularExpressions, how can i do?

enter image description here

答:
I suggest using Newtonsoft.JSON instead of trying to parse it with Regex.

It is as easy to use as this example from the website:

```<code>string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
// Bad Boys
</code>```

Body:
I suggest using Newtonsoft.JSON instead of trying to parse it with Regex.

It is as easy to use as this example from the website:

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
// Bad Boys


问:
Hashchange jQuery plugin - obtaining the current hash when clicking an anchor
I'm using hashchange jQuery plugin from http://benalman.com/projects/jquery-hashchange-plugin/ to hook up an event when the window's location.hash changes.

I'd like to trigger a function when the hash changes that passes the new hash value (obtained with event.fragment) and the current hash value (the value just before the event is fired).

Here's a snipped of what I'd like to achieve:

```<code>$(window).bind('hashchange', function(event){
   myFunction(event.fragment, /* currentHash */);
});
</code>```

Is this possible?

答:
There's a property on location:

```<code>$(window).bind('hashchange', function(event){
   myFunction(event.fragment, location.hash);
});
</code>```

Or, store it yourself:

```<code>var lastHash = location.hash;                 //set it initially
$(window).bind('hashchange', function(event){
   myFunction(event.fragment, hashLash);      //previous hash
   lastHash = location.hash;                  //update it
});
</code>```

Body:
I'm using hashchange jQuery plugin from http://benalman.com/projects/jquery-hashchange-plugin/ to hook up an event when the window's location.hash changes.

I'd like to trigger a function when the hash changes that passes the new hash value (obtained with event.fragment) and the current hash value (the value just before the event is fired).

Here's a snipped of what I'd like to achieve:

$(window).bind('hashchange', function(event){
   myFunction(event.fragment, /* currentHash */);
});

Is this possible?

Body:
There's a property on location:

$(window).bind('hashchange', function(event){
   myFunction(event.fragment, location.hash);
});

Or, store it yourself:

var lastHash = location.hash;                 //set it initially
$(window).bind('hashchange', function(event){
   myFunction(event.fragment, hashLash);      //previous hash
   lastHash = location.hash;                  //update it
});


问:
Sonar on Travis: ERROR You must define the following mandatory properties for 'Unknown'
I'm trying to setup SonarQube on Travis for a Java project by following Travis documentation https://docs.travis-ci.com/user/sonarqube/

My resulting my .travis.yml file is as follows:

```<code>sudo: required
cache:
  directories:
    - $HOME/.m2
language: java
services:
  - docker
jdk:
  - oraclejdk8
os:
  - linux
env:
  matrix:
    - NEO_VERSION=3.0.9
      WITH_DOCKER=true
      EXTRA_PROFILES=-Pwith-neo4j-io
    - NEO_VERSION=3.1.2
      WITH_DOCKER=true
      EXTRA_PROFILES=-Pwith-neo4j-io
addons:
  sonarqube:
    organization: ${SONAR_ORGANIZATION}
    token:
      secure: ${SONAR_TOKEN}
    branches:
      - master
      - sonarqube
before_script:
script:
  - build/run.sh
  - sonar-scanner
install: true
after_success:
  - mvn clean test jacoco:report coveralls:report
  - "[ ${TRAVIS_PULL_REQUEST} = 'false' ] && [[ ${TRAVIS_BRANCH} = 'master' ]] && mvn clean deploy -DskipTests --settings ./deploy-settings.xml"
</code>```

My resulting sonar-scanner.properties file is:

```<code># must be unique in a given SonarQube instance
sonar.projectKey=liquigraph
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=Liquigraph
sonar.projectVersion=1.0

# Set modules IDs
sonar.modules=liquigraph-cli,liquigraph-core,liquigraph-maven-plugin,liquigraph-spring-boot-starter,liquigraph-examples

# Modules inherit properties set at parent level
sonar.sources=src
sonar.sourceEncoding=UTF-8
sonar.language=java
</code>```

But when I commit and Travis start building, sonar job fails with following logs:

```<code>. . .
$ sonar-scanner
INFO: Scanner configuration file: /home/travis/.sonarscanner/sonar-scanner-2.8/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 2.8
INFO: Java 1.8.0_111 Oracle Corporation (64-bit)
INFO: Linux 4.4.0-51-generic amd64
INFO: User cache: /home/travis/.sonar/cache
. . .
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 13.770s
INFO: Final Memory: 64M/505M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.sources
ERROR:
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.
</code>```

I did put my sonar-scanner.properties in the root of my project folder. Why travis cannot find and instead is using /home/travis/.sonarscanner/sonar-scanner-2.8/conf/sonar-scanner.properties?

答:
If your project is a Maven project, then simply use the Scanner for Maven: mvn sonar:sonar

Look at https://about.sonarqube.com/get-started/ for details.

If you really want to use the SonarQube Scanner, then your issue is that you are not using the correct name for the configuration file. It should be sonar-project.properties.

Body:
I'm trying to setup SonarQube on Travis for a Java project by following Travis documentation https://docs.travis-ci.com/user/sonarqube/

My resulting my .travis.yml file is as follows:

sudo: required
cache:
  directories:
    - $HOME/.m2
language: java
services:
  - docker
jdk:
  - oraclejdk8
os:
  - linux
env:
  matrix:
    - NEO_VERSION=3.0.9
      WITH_DOCKER=true
      EXTRA_PROFILES=-Pwith-neo4j-io
    - NEO_VERSION=3.1.2
      WITH_DOCKER=true
      EXTRA_PROFILES=-Pwith-neo4j-io
addons:
  sonarqube:
    organization: ${SONAR_ORGANIZATION}
    token:
      secure: ${SONAR_TOKEN}
    branches:
      - master
      - sonarqube
before_script:
script:
  - build/run.sh
  - sonar-scanner
install: true
after_success:
  - mvn clean test jacoco:report coveralls:report
  - "[ ${TRAVIS_PULL_REQUEST} = 'false' ] && [[ ${TRAVIS_BRANCH} = 'master' ]] && mvn clean deploy -DskipTests --settings ./deploy-settings.xml"

My resulting sonar-scanner.properties file is:

# must be unique in a given SonarQube instance
sonar.projectKey=liquigraph
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=Liquigraph
sonar.projectVersion=1.0

# Set modules IDs
sonar.modules=liquigraph-cli,liquigraph-core,liquigraph-maven-plugin,liquigraph-spring-boot-starter,liquigraph-examples

# Modules inherit properties set at parent level
sonar.sources=src
sonar.sourceEncoding=UTF-8
sonar.language=java

But when I commit and Travis start building, sonar job fails with following logs:

. . .
$ sonar-scanner
INFO: Scanner configuration file: /home/travis/.sonarscanner/sonar-scanner-2.8/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 2.8
INFO: Java 1.8.0_111 Oracle Corporation (64-bit)
INFO: Linux 4.4.0-51-generic amd64
INFO: User cache: /home/travis/.sonar/cache
. . .
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 13.770s
INFO: Final Memory: 64M/505M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.sources
ERROR:
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

I did put my sonar-scanner.properties in the root of my project folder. Why travis cannot find and instead is using /home/travis/.sonarscanner/sonar-scanner-2.8/conf/sonar-scanner.properties?


问:
Python custom modules management
I have a script (dump_ora_shelve.py) which retrieves the data from shelve-storage by specifying the key, e.g.:

```<code>def get_shelve_users(field):
    import shelve
    db = shelve.open('oracle-shelve')
    for key in db:
        if key == field:
             return db[key]
    db.close()
</code>```

The data is retrieved just fine:

```<code>print(get_shelve_users('db_users'))
> {'SYS': 'sysdba'}
print(get_shelve_users('oratab'))
> ['orcl:/u01/app']
</code>```

There is another script which should do the same thing (retrieve the data with key specified) that has dump_ora_shelve imported, but the value returns is Null:

```<code>from before_OOP.dump_ora_shelve import get_shelve_users
print(get_shelve_users('db_users'))
> Null
print(get_shelve_users('oratab'))
> Null
</code>```

The file being imported is located one level above from the file it is importing to.

Please note if I copy both files to the same location import and then function works just fine.

答:
You could provide the full pathname to shelve.open. Remember that inside a module, __file__ is the path of where the source file resides. So you can use that to construct the full pathname.

Typically you will have something like this:

```<code>BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
</code>```

Use os.path.join to concatenate the directory and the filename. Note the use of os.path.dirname and os.path.abspath.

So you can say:

```<code>BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
shelve_db = os.path.join(BASE_DIR, 'oracle-shelve')
db = shelve.open('oracle-shelve')
</code>```

This assumes that the orache-shelve file is in the same folder as the module in which the get_shelve_users function is (dump_ora_shelve.py).

Don't forget the __file__. That is what makes the whole thing tick, i.e. makes your program insulated from whatever its current directory is.

Title: Python custom modules management

Body:
I have a script (dump_ora_shelve.py) which retrieves the data from shelve-storage by specifying the key, e.g.:

def get_shelve_users(field):
    import shelve
    db = shelve.open('oracle-shelve')
    for key in db:
        if key == field:
             return db[key]
    db.close()

The data is retrieved just fine:

print(get_shelve_users('db_users'))
> {'SYS': 'sysdba'}
print(get_shelve_users('oratab'))
> ['orcl:/u01/app']

There is another script which should do the same thing (retrieve the data with key specified) that has dump_ora_shelve imported, but the value returns is Null:

from before_OOP.dump_ora_shelve import get_shelve_users
print(get_shelve_users('db_users'))
> Null
print(get_shelve_users('oratab'))
> Null

The file being imported is located one level above from the file it is importing to.

Please note if I copy both files to the same location import and then function works just fine.

Body:
You could provide the full pathname to shelve.open. Remember that inside a module, __file__ is the path of where the source file resides. So you can use that to construct the full pathname.

Typically you will have something like this:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Use os.path.join to concatenate the directory and the filename. Note the use of os.path.dirname and os.path.abspath.

So you can say:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
shelve_db = os.path.join(BASE_DIR, 'oracle-shelve')
db = shelve.open('oracle-shelve')

This assumes that the orache-shelve file is in the same folder as the module in which the get_shelve_users function is (dump_ora_shelve.py).

Don't forget the __file__. That is what makes the whole thing tick, i.e. makes your program insulated from whatever its current directory is.


问:
Translate WinApi Crypt to the .NET Framework System.Security.Cryptography
I would like to ask for a help with refactoring following C# code.

The target is to remove all external WinApi call and replace them with methods from System.Security.Cryptography namespace.

```<code>private static IntPtr GenerateKey(IntPtr hCryptProv, byte[] keyData)
{
    var hHash = IntPtr.Zero;
    Win32.CryptCreateHash(hCryptProv, Win32.CALG_MD5, IntPtr.Zero, 0, ref hHash);
    var len = (uint)keyData.Length;
    Win32.CryptHashData(hHash, keyData, len, 0);
    var hKey = IntPtr.Zero;
    Win32.CryptDeriveKey(hCryptProv, Win32.CALG_3DES, hHash, 0, ref hKey);
    if (hHash != IntPtr.Zero) Win32.CryptDestroyHash(hHash);
    return hKey;
}

public static byte[] Encrypt(byte[] dataToEncrypt)
{
    var keyData = Encoding.ASCII.GetBytes("{2B9B4443-74CE-42A8-8803-076B136B5967}");
    var size = (uint)dataToEncrypt.Length;
    var buffer = new byte[size * 2];
    Array.Copy(dataToEncrypt, 0, buffer, 0, size);
    var hCryptProv = IntPtr.Zero;
    bool gotcsp = Win32.CryptAcquireContext(ref hCryptProv, null, null, Win32.PROV_RSA_FULL, Win32.CRYPT_VERIFYCONTEXT | Win32.CRYPT_MACHINE_KEYSET);
    if (!gotcsp)
    {
        Win32.CryptAcquireContext(ref hCryptProv, null, null, Win32.PROV_RSA_FULL, Win32.CRYPT_NEWKEYSET | Win32.CRYPT_VERIFYCONTEXT | Win32.CRYPT_MACHINE_KEYSET);
    }

    if (hCryptProv == IntPtr.Zero) return null;

    var hKey = GenerateKey(hCryptProv, keyData);
    Win32.CryptEncrypt(hKey, IntPtr.Zero, 1, 0, buffer, ref size, size*2);
    var encryptedData = new byte[size];
    Array.Copy(buffer, 0, encryptedData, 0, size);
    if (hKey != IntPtr.Zero) Win32.CryptDestroyKey(hKey);
    if (hCryptProv != IntPtr.Zero) Win32.CryptReleaseContext(hCryptProv, 0);
    return encryptedData;
}
/// <summary>
/// WinAPI Imports
/// </summary>
internal class Win32
{
    public const uint PROV_RSA_FULL = 1;
    public const uint NTE_BAD_KEYSET = 0x80090016;
    public const uint CRYPT_NEWKEYSET = 0x00000008;
    public const uint CRYPT_VERIFYCONTEXT = 0xF0000000;
    public const uint CRYPT_MACHINE_KEYSET = 0x00000020;

    public const uint ALG_CLASS_HASH = (4 << 13);
    public const uint ALG_SID_MD5 = 3;
    public const uint CALG_MD5 = (ALG_CLASS_HASH | ALG_SID_MD5);
    public const uint ALG_CLASS_DATA_ENCRYPT = (3 << 13);
    public const uint ALG_TYPE_BLOCK = (3 << 9);
    public const uint ALG_SID_3DES = 3;
    public const uint CALG_3DES = (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_3DES);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool CryptAcquireContext(ref IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDestroyKey(IntPtr hKey);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptCreateHash(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, ref IntPtr hHash);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptHashData(IntPtr hHash, [In, Out] byte[] pbData, uint dwDataLen, uint dwSize);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDestroyHash(IntPtr hHash);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDeriveKey(IntPtr hProv, uint Algid, IntPtr hHash, uint dwFlags, ref IntPtr hKey);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptEncrypt(IntPtr hKey, IntPtr hHash, int Final, uint dwFlags, [In, Out] byte[] pbData, ref uint pdwDataLen, uint dwBufLen);
}
</code>```

I have tried different combinations, options and encryption providers, and there is no problem to create similar method using System.Security.Cryptography, but the problem is that I need a method that will replace a code.
That means that with the same data passed for encryption I must get the same result. And here is a problem. My knowledge of encryption are definitely is not so deep to take into account all nuances of this method.

Can you help me with this issue? I don't mean to give me a link to encryption tutorial, but to tell me what methods with which options I should to use.

[2017-03-28 11:27GMT] Additional information:

I really do not think that it will helps, but there is one of my experimental code that I finish with:

```<code>public static List<byte> Encrypt(byte[] toEncrypt)
{
    var databytes = Encoding.ASCII.GetBytes("{2B9B4443-74CE-42A8-8803-076B136B5967}");
    var hashmd5 = new MD5CryptoServiceProvider();
    var keyArray = hashmd5.ComputeHash(databytes);
    hashmd5.Clear();
    var pdb = new PasswordDeriveBytes(keyArray, new byte[0]);
    var hashKey = pdb.CryptDeriveKey("TripleDES", "MD5", 0, new byte[8]);

    var tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = hashKey;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;
    var cTransform = tdes.CreateEncryptor();
    var resultArray = cTransform.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
    tdes.Clear();
    return resultArray.ToList();
}
</code>```

There was many other variations but no any that give me correct result:

Source data:

```<code>private byte[] dataToEncrypt = {224,111,176,138,238,238,238,239,115,109,201,144,89,58,161,0,0,0,0,0,0,0,0};
</code>```

Original function reurns:

```<code>private byte[] originalResult = {31,173,65,161,199,249,73,200,210,74,156,21,36,160,94,137,71,205,15,206,99,105,40,83};
</code>```

Sample function returns:

```<code>private byte[] sampleResult = {211,29,187,125,82,9,240,177,199,133,135,7,132,166,166,164,189,36,126,186,104,79,53,159};
</code>```

答:
I lost at least one hour because I thought your code was using RSA (I was mislead by the PROV_RSA_FULL)... In truth it is only doing 3DES encryption...

```<code>var keyData = Encoding.ASCII.GetBytes("{2B9B4443-74CE-42A8-8803-076B136B5967}");
byte[] key;

using (PasswordDeriveBytes pdb = new PasswordDeriveBytes(keyData, null))
{
    key = pdb.CryptDeriveKey("TripleDES", "MD5", 0, new byte[8]);
    //Debug.WriteLine(BitConverter.ToString(key));
}

using (var prov = new TripleDESCryptoServiceProvider())
{
    using (var encryptor = prov.CreateEncryptor(key, new byte[8]))
    {
        byte[] encrypted = encryptor.TransformFinalBlock(bytes2, 0, bytes2.Length);
        //Debug.WriteLine(BitConverter.ToString(encrypted));
    }
}
</code>```

Just out of curiousity I'll post a modified version of the original C# code... I wanted to debug a little the generated key, but it is complex to export it in CryptoAPI, and then the original code wasn't like I would have written it :-)

```<code>private static IntPtr GenerateKey(IntPtr hProv, byte[] keyData)
{
    var hHash = IntPtr.Zero;

    try
    {
        bool check = Win32.CryptCreateHash(hProv, Win32.CALG_MD5, IntPtr.Zero, 0, out hHash);

        if (!check)
        {
            throw new Win32Exception();
        }

        check = Win32.CryptHashData(hHash, keyData, keyData.Length, 0);

        if (!check)
        {
            throw new Win32Exception();
        }

        IntPtr hKey;

        check = Win32.CryptDeriveKey(hProv, Win32.CALG_3DES, hHash, Win32.CRYPT_EXPORTABLE, out hKey);

        if (!check)
        {
            throw new Win32Exception();
        }

        return hKey;
    }
    finally
    {
        if (hHash != IntPtr.Zero)
        {
            Win32.CryptDestroyHash(hHash);
        }
    }
}

public static byte[] Encrypt(byte[] plainText)
{
    var keyData = Encoding.ASCII.GetBytes("{2B9B4443-74CE-42A8-8803-076B136B5967}");

    IntPtr hCryptProv = IntPtr.Zero;
    IntPtr hKey = IntPtr.Zero;

    try
    {
        bool check = Win32.CryptAcquireContext(out hCryptProv, null, null, Win32.PROV_RSA_FULL, Win32.CRYPT_VERIFYCONTEXT | Win32.CRYPT_MACHINE_KEYSET);

        if (!check)
        {
            check = Win32.CryptAcquireContext(out hCryptProv, null, null, Win32.PROV_RSA_FULL, Win32.CRYPT_NEWKEYSET | Win32.CRYPT_VERIFYCONTEXT | Win32.CRYPT_MACHINE_KEYSET);

            if (!check)
            {
                throw new Win32Exception();
            }
        }

        hKey = GenerateKey(hCryptProv, keyData);

        //byte[] key = ExportSymmetricKey(hCryptProv, hKey);
        //Debug.WriteLine(BitConverter.ToString(key));

        var size = plainText.Length;
        check = Win32.CryptEncrypt(hKey, IntPtr.Zero, 1, 0, null, ref size, 0);

        if (!check)
        {
            throw new Win32Exception();
        }

        var cypherText = new byte[size];

        Array.Copy(plainText, cypherText, plainText.Length);

        size = plainText.Length;

        check = Win32.CryptEncrypt(hKey, IntPtr.Zero, 1, 0, cypherText, ref size, cypherText.Length);

        if (!check)
        {
            throw new Win32Exception();
        }

        return cypherText;
    }
    finally
    {
        if (hKey != IntPtr.Zero)
        {
            Win32.CryptDestroyKey(hKey);
        }

        if (hCryptProv != IntPtr.Zero)
        {
            Win32.CryptReleaseContext(hCryptProv, 0);
        }
    }
}

// Based on https://books.google.it/books?id=aL3P3eJdiREC&pg=PT271&lpg=PT271&dq=PROV_RSA_FULL+CryptEncrypt&source=bl&ots=STsuConTHr&sig=W-BWwch8aZ-RqFb8N67rMHTrqYc&hl=it&sa=X&ved=0ahUKEwit2qKnlfvSAhWCtRQKHbL9BbQQ6AEIQzAF#v=onepage&q=PROV_RSA_FULL%20CryptEncrypt&f=false
// Page 248
private static byte[] ExportSymmetricKey(IntPtr hProv, IntPtr hKey)
{
    IntPtr hExpKey = IntPtr.Zero;

    try
    {
        bool check = Win32.CryptGenKey(hProv, 1 /* AT_KEYEXCHANGE */, 1024 << 16, out hExpKey);

        if (!check)
        {
            throw new Win32Exception();
        }

        int size = 0;
        check = Win32.CryptExportKey(hKey, hExpKey, 1 /* SIMPLEBLOB */, 0, null, ref size);

        if (!check)
        {
            throw new Win32Exception();
        }

        var bytes = new byte[size];

        check = Win32.CryptExportKey(hKey, hExpKey, 1 /* SIMPLEBLOB */, 0, bytes, ref size);

        if (!check)
        {
            throw new Win32Exception();
        }

        // The next lines could be optimized by using a CryptDecrypt
        // that accepts a IntPtr and adding directly 12 to the ref bytes
        // instead of copying around the byte array

        // 12 == sizeof(BLOBHEADER) + sizeof(ALG_ID)
        var bytes2 = new byte[size - 12];

        Array.Copy(bytes, 12, bytes2, 0, bytes2.Length);

        bytes = bytes2;
        bytes2 = null;

        check = Win32.CryptDecrypt(hExpKey, IntPtr.Zero, true, 0, bytes, ref size);

        if (!check)
        {
            throw new Win32Exception();
        }

        Array.Resize(ref bytes, size);

        return bytes;
    }
    finally
    {
        if (hExpKey != IntPtr.Zero)
        {
            Win32.CryptDestroyKey(hExpKey);
        }
    }
}

/// <summary>
/// WinAPI Imports
/// </summary>
internal class Win32
{
    public const uint PROV_RSA_FULL = 1;
    public const uint NTE_BAD_KEYSET = 0x80090016;
    public const uint CRYPT_NEWKEYSET = 0x00000008;
    public const uint CRYPT_VERIFYCONTEXT = 0xF0000000;
    public const uint CRYPT_MACHINE_KEYSET = 0x00000020;

    public const uint CRYPT_EXPORTABLE = 1;

    public const uint ALG_CLASS_HASH = (4 << 13);
    public const uint ALG_SID_MD5 = 3;
    public const uint CALG_MD5 = (ALG_CLASS_HASH | ALG_SID_MD5);
    public const uint ALG_CLASS_DATA_ENCRYPT = (3 << 13);
    public const uint ALG_TYPE_BLOCK = (3 << 9);
    public const uint ALG_SID_3DES = 3;
    public const uint CALG_3DES = (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_3DES);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool CryptAcquireContext(out IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDestroyKey(IntPtr hKey);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptCreateHash(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, out IntPtr hHash);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptHashData(IntPtr hHash, [In, Out] byte[] pbData, int dwDataLen, uint dwSize);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDestroyHash(IntPtr hHash);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDeriveKey(IntPtr hProv, uint Algid, IntPtr hHash, uint dwFlags, out IntPtr hKey);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptGenKey(IntPtr hProv, uint Algid, uint dwFlags, out IntPtr hKey);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptEncrypt(IntPtr hKey, IntPtr hHash, int final, uint dwFlags, [In, Out] byte[] pbData, ref int pdwDataLen, int dwBufLen);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptExportKey(IntPtr hKey, IntPtr hExpKey, uint dwBlobType, uint dwFlags, [Out] byte[] pbData, ref int pdwDataLen);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptGetKeyParam(IntPtr hKey, uint dwParam, [Out] byte[] pbData, ref int pdwDataLen, uint dwFlags);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDecrypt(IntPtr hKey, IntPtr hHash, bool final, uint dwFlags, [In, Out] byte[] pbData, ref int pdwDataLen);
}
</code>```

Body:
I would like to ask for a help with refactoring following C# code.

The target is to remove all external WinApi call and replace them with methods from System.Security.Cryptography namespace.

private static IntPtr GenerateKey(IntPtr hCryptProv, byte[] keyData)
{
    var hHash = IntPtr.Zero;
    Win32.CryptCreateHash(hCryptProv, Win32.CALG_MD5, IntPtr.Zero, 0, ref hHash);
    var len = (uint)keyData.Length;
    Win32.CryptHashData(hHash, keyData, len, 0);
    var hKey = IntPtr.Zero;
    Win32.CryptDeriveKey(hCryptProv, Win32.CALG_3DES, hHash, 0, ref hKey);
    if (hHash != IntPtr.Zero) Win32.CryptDestroyHash(hHash);
    return hKey;
}

public static byte[] Encrypt(byte[] dataToEncrypt)
{
    var keyData = Encoding.ASCII.GetBytes("{2B9B4443-74CE-42A8-8803-076B136B5967}");
    var size = (uint)dataToEncrypt.Length;
    var buffer = new byte[size * 2];
    Array.Copy(dataToEncrypt, 0, buffer, 0, size);
    var hCryptProv = IntPtr.Zero;
    bool gotcsp = Win32.CryptAcquireContext(ref hCryptProv, null, null, Win32.PROV_RSA_FULL, Win32.CRYPT_VERIFYCONTEXT | Win32.CRYPT_MACHINE_KEYSET);
    if (!gotcsp)
    {
        Win32.CryptAcquireContext(ref hCryptProv, null, null, Win32.PROV_RSA_FULL, Win32.CRYPT_NEWKEYSET | Win32.CRYPT_VERIFYCONTEXT | Win32.CRYPT_MACHINE_KEYSET);
    }

    if (hCryptProv == IntPtr.Zero) return null;

    var hKey = GenerateKey(hCryptProv, keyData);
    Win32.CryptEncrypt(hKey, IntPtr.Zero, 1, 0, buffer, ref size, size*2);
    var encryptedData = new byte[size];
    Array.Copy(buffer, 0, encryptedData, 0, size);
    if (hKey != IntPtr.Zero) Win32.CryptDestroyKey(hKey);
    if (hCryptProv != IntPtr.Zero) Win32.CryptReleaseContext(hCryptProv, 0);
    return encryptedData;
}
///
/// WinAPI Imports
///
internal class Win32
{
    public const uint PROV_RSA_FULL = 1;
    public const uint NTE_BAD_KEYSET = 0x80090016;
    public const uint CRYPT_NEWKEYSET = 0x00000008;
    public const uint CRYPT_VERIFYCONTEXT = 0xF0000000;
    public const uint CRYPT_MACHINE_KEYSET = 0x00000020;

    public const uint ALG_CLASS_HASH = (4 << 13);
    public const uint ALG_SID_MD5 = 3;
    public const uint CALG_MD5 = (ALG_CLASS_HASH | ALG_SID_MD5);
    public const uint ALG_CLASS_DATA_ENCRYPT = (3 << 13);
    public const uint ALG_TYPE_BLOCK = (3 << 9);
    public const uint ALG_SID_3DES = 3;
    public const uint CALG_3DES = (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_3DES);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool CryptAcquireContext(ref IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDestroyKey(IntPtr hKey);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptCreateHash(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, ref IntPtr hHash);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptHashData(IntPtr hHash, [In, Out] byte[] pbData, uint dwDataLen, uint dwSize);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDestroyHash(IntPtr hHash);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDeriveKey(IntPtr hProv, uint Algid, IntPtr hHash, uint dwFlags, ref IntPtr hKey);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptEncrypt(IntPtr hKey, IntPtr hHash, int Final, uint dwFlags, [In, Out] byte[] pbData, ref uint pdwDataLen, uint dwBufLen);
}

I have tried different combinations, options and encryption providers, and there is no problem to create similar method using System.Security.Cryptography, but the problem is that I need a method that will replace a code.
That means that with the same data passed for encryption I must get the same result. And here is a problem. My knowledge of encryption are definitely is not so deep to take into account all nuances of this method.

Can you help me with this issue? I don't mean to give me a link to encryption tutorial, but to tell me what methods with which options I should to use.

[2017-03-28 11:27GMT] Additional information:

I really do not think that it will helps, but there is one of my experimental code that I finish with:

public static List<byte> Encrypt(byte[] toEncrypt)
{
    var databytes = Encoding.ASCII.GetBytes("{2B9B4443-74CE-42A8-8803-076B136B5967}");
    var hashmd5 = new MD5CryptoServiceProvider();
    var keyArray = hashmd5.ComputeHash(databytes);
    hashmd5.Clear();
    var pdb = new PasswordDeriveBytes(keyArray, new byte[0]);
    var hashKey = pdb.CryptDeriveKey("TripleDES", "MD5", 0, new byte[8]);

    var tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = hashKey;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;
    var cTransform = tdes.CreateEncryptor();
    var resultArray = cTransform.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
    tdes.Clear();
    return resultArray.ToList();
}

There was many other variations but no any that give me correct result:

Source data:

private byte[] dataToEncrypt = {224,111,176,138,238,238,238,239,115,109,201,144,89,58,161,0,0,0,0,0,0,0,0};

Original function reurns:

private byte[] originalResult = {31,173,65,161,199,249,73,200,210,74,156,21,36,160,94,137,71,205,15,206,99,105,40,83};

Sample function returns:

private byte[] sampleResult = {211,29,187,125,82,9,240,177,199,133,135,7,132,166,166,164,189,36,126,186,104,79,53,159};

Body:
I lost at least one hour because I thought your code was using RSA (I was mislead by the PROV_RSA_FULL)... In truth it is only doing 3DES encryption...

var keyData = Encoding.ASCII.GetBytes("{2B9B4443-74CE-42A8-8803-076B136B5967}");
byte[] key;

using (PasswordDeriveBytes pdb = new PasswordDeriveBytes(keyData, null))
{
    key = pdb.CryptDeriveKey("TripleDES", "MD5", 0, new byte[8]);
    //Debug.WriteLine(BitConverter.ToString(key));
}

using (var prov = new TripleDESCryptoServiceProvider())
{
    using (var encryptor = prov.CreateEncryptor(key, new byte[8]))
    {
        byte[] encrypted = encryptor.TransformFinalBlock(bytes2, 0, bytes2.Length);
        //Debug.WriteLine(BitConverter.ToString(encrypted));
    }
}

Just out of curiousity I'll post a modified version of the original C# code... I wanted to debug a little the generated key, but it is complex to export it in CryptoAPI, and then the original code wasn't like I would have written it :-)

private static IntPtr GenerateKey(IntPtr hProv, byte[] keyData)
{
    var hHash = IntPtr.Zero;

    try
    {
        bool check = Win32.CryptCreateHash(hProv, Win32.CALG_MD5, IntPtr.Zero, 0, out hHash);

        if (!check)
        {
            throw new Win32Exception();
        }

        check = Win32.CryptHashData(hHash, keyData, keyData.Length, 0);

        if (!check)
        {
            throw new Win32Exception();
        }

        IntPtr hKey;

        check = Win32.CryptDeriveKey(hProv, Win32.CALG_3DES, hHash, Win32.CRYPT_EXPORTABLE, out hKey);

        if (!check)
        {
            throw new Win32Exception();
        }

        return hKey;
    }
    finally
    {
        if (hHash != IntPtr.Zero)
        {
            Win32.CryptDestroyHash(hHash);
        }
    }
}

public static byte[] Encrypt(byte[] plainText)
{
    var keyData = Encoding.ASCII.GetBytes("{2B9B4443-74CE-42A8-8803-076B136B5967}");

    IntPtr hCryptProv = IntPtr.Zero;
    IntPtr hKey = IntPtr.Zero;

    try
    {
        bool check = Win32.CryptAcquireContext(out hCryptProv, null, null, Win32.PROV_RSA_FULL, Win32.CRYPT_VERIFYCONTEXT | Win32.CRYPT_MACHINE_KEYSET);

        if (!check)
        {
            check = Win32.CryptAcquireContext(out hCryptProv, null, null, Win32.PROV_RSA_FULL, Win32.CRYPT_NEWKEYSET | Win32.CRYPT_VERIFYCONTEXT | Win32.CRYPT_MACHINE_KEYSET);

            if (!check)
            {
                throw new Win32Exception();
            }
        }

        hKey = GenerateKey(hCryptProv, keyData);

        //byte[] key = ExportSymmetricKey(hCryptProv, hKey);
        //Debug.WriteLine(BitConverter.ToString(key));

        var size = plainText.Length;
        check = Win32.CryptEncrypt(hKey, IntPtr.Zero, 1, 0, null, ref size, 0);

        if (!check)
        {
            throw new Win32Exception();
        }

        var cypherText = new byte[size];

        Array.Copy(plainText, cypherText, plainText.Length);

        size = plainText.Length;

        check = Win32.CryptEncrypt(hKey, IntPtr.Zero, 1, 0, cypherText, ref size, cypherText.Length);

        if (!check)
        {
            throw new Win32Exception();
        }

        return cypherText;
    }
    finally
    {
        if (hKey != IntPtr.Zero)
        {
            Win32.CryptDestroyKey(hKey);
        }

        if (hCryptProv != IntPtr.Zero)
        {
            Win32.CryptReleaseContext(hCryptProv, 0);
        }
    }
}

// Based on https://books.google.it/books?id=aL3P3eJdiREC&pg=PT271&lpg=PT271&dq=PROV_RSA_FULL+CryptEncrypt&source=bl&ots=STsuConTHr&sig=W-BWwch8aZ-RqFb8N67rMHTrqYc&hl=it&sa=X&ved=0ahUKEwit2qKnlfvSAhWCtRQKHbL9BbQQ6AEIQzAF#v=onepage&q=PROV_RSA_FULL%20CryptEncrypt&f=false
// Page 248
private static byte[] ExportSymmetricKey(IntPtr hProv, IntPtr hKey)
{
    IntPtr hExpKey = IntPtr.Zero;

    try
    {
        bool check = Win32.CryptGenKey(hProv, 1 /* AT_KEYEXCHANGE */, 1024 << 16, out hExpKey);

        if (!check)
        {
            throw new Win32Exception();
        }

        int size = 0;
        check = Win32.CryptExportKey(hKey, hExpKey, 1 /* SIMPLEBLOB */, 0, null, ref size);

        if (!check)
        {
            throw new Win32Exception();
        }

        var bytes = new byte[size];

        check = Win32.CryptExportKey(hKey, hExpKey, 1 /* SIMPLEBLOB */, 0, bytes, ref size);

        if (!check)
        {
            throw new Win32Exception();
        }

        // The next lines could be optimized by using a CryptDecrypt
        // that accepts a IntPtr and adding directly 12 to the ref bytes
        // instead of copying around the byte array

        // 12 == sizeof(BLOBHEADER) + sizeof(ALG_ID)
        var bytes2 = new byte[size - 12];

        Array.Copy(bytes, 12, bytes2, 0, bytes2.Length);

        bytes = bytes2;
        bytes2 = null;

        check = Win32.CryptDecrypt(hExpKey, IntPtr.Zero, true, 0, bytes, ref size);

        if (!check)
        {
            throw new Win32Exception();
        }

        Array.Resize(ref bytes, size);

        return bytes;
    }
    finally
    {
        if (hExpKey != IntPtr.Zero)
        {
            Win32.CryptDestroyKey(hExpKey);
        }
    }
}

/// <summary>
/// WinAPI Imports
/// </summary>
internal class Win32
{
    public const uint PROV_RSA_FULL = 1;
    public const uint NTE_BAD_KEYSET = 0x80090016;
    public const uint CRYPT_NEWKEYSET = 0x00000008;
    public const uint CRYPT_VERIFYCONTEXT = 0xF0000000;
    public const uint CRYPT_MACHINE_KEYSET = 0x00000020;

    public const uint CRYPT_EXPORTABLE = 1;

    public const uint ALG_CLASS_HASH = (4 << 13);
    public const uint ALG_SID_MD5 = 3;
    public const uint CALG_MD5 = (ALG_CLASS_HASH | ALG_SID_MD5);
    public const uint ALG_CLASS_DATA_ENCRYPT = (3 << 13);
    public const uint ALG_TYPE_BLOCK = (3 << 9);
    public const uint ALG_SID_3DES = 3;
    public const uint CALG_3DES = (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_3DES);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool CryptAcquireContext(out IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDestroyKey(IntPtr hKey);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptCreateHash(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, out IntPtr hHash);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptHashData(IntPtr hHash, [In, Out] byte[] pbData, int dwDataLen, uint dwSize);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDestroyHash(IntPtr hHash);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDeriveKey(IntPtr hProv, uint Algid, IntPtr hHash, uint dwFlags, out IntPtr hKey);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptGenKey(IntPtr hProv, uint Algid, uint dwFlags, out IntPtr hKey);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptEncrypt(IntPtr hKey, IntPtr hHash, int final, uint dwFlags, [In, Out] byte[] pbData, ref int pdwDataLen, int dwBufLen);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptExportKey(IntPtr hKey, IntPtr hExpKey, uint dwBlobType, uint dwFlags, [Out] byte[] pbData, ref int pdwDataLen);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptGetKeyParam(IntPtr hKey, uint dwParam, [Out] byte[] pbData, ref int pdwDataLen, uint dwFlags);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool CryptDecrypt(IntPtr hKey, IntPtr hHash, bool final, uint dwFlags, [In, Out] byte[] pbData, ref int pdwDataLen);
}


问:
Why -1 >> 1 is -1 ? And 1 >> 1 is 0!
I have next code:

```<code>std::cout << (-10 >> 1) << std::endl;
std::cout << (-9 >> 1) << std::endl;
std::cout << (-8 >> 1) << std::endl;
std::cout << (-7 >> 1) << std::endl;
std::cout << (-6 >> 1) << std::endl;
std::cout << (-5 >> 1) << std::endl;
std::cout << (-4 >> 1) << std::endl;
std::cout << (-3 >> 1) << std::endl;
std::cout << (-2 >> 1) << std::endl;
std::cout << (-1 >> 1) << std::endl;
</code>```

The result is:

```<code>-5
-5
-4
-4
-3
-3
-2
-2
-1
-1
</code>```

But why?

-1 is 1111 1111 (for 1 byte), -1 >> 1 must be : 1011 1111 and it is not -1 or 0! (sign-bit is not shifted, I know)

Can someone explain to me how this works?

答:
-1 >> 1 must be : 1011 1111

If that were true, -10 >> 1 would be 10111011 == -69 in two's complement.  Not a very useful result!

Although the language behaviour is undefined (so the result, useful or otherwise cannot be relied upon), the common behaviour (and that exhibited in this case) is to perform "sign-extension".

It is not true that the "sign-bit is not shifted", it is shifted, and the vacated bit is filled with a value equal to the sign bit.  This behavour both preserves the sign and provides the 'expected' divide-by-two operation that you have observed for all values except -1.  For negative values -1 is the 'terminal value' of a right shift, as zero is for positive numbers.  That is to say right shift of a negative number tends to -1, and a positive number tends to zero.

Title: Why -1 >> 1 is -1 ? And 1 >> 1 is 0!

Body:
I have next code:

std::cout << (-10 >> 1) << std::endl;
std::cout << (-9 >> 1) << std::endl;
std::cout << (-8 >> 1) << std::endl;
std::cout << (-7 >> 1) << std::endl;
std::cout << (-6 >> 1) << std::endl;
std::cout << (-5 >> 1) << std::endl;
std::cout << (-4 >> 1) << std::endl;
std::cout << (-3 >> 1) << std::endl;
std::cout << (-2 >> 1) << std::endl;
std::cout << (-1 >> 1) << std::endl;

The result is:

-5
-5
-4
-4
-3
-3
-2
-2
-1
-1

But why?

-1 is 1111 1111 (for 1 byte), -1 >> 1 must be : 1011 1111 and it is not -1 or 0! (sign-bit is not shifted, I know)

Can someone explain to me how this works?


问:
Not receiving claims from WSO2 Identity Server (v5.1.0) when used with Oauth2
I am using WSO2 Identity Server as the identity provider for our websites. The websites are being developed in ASP.net and we are using in-built Owin-Katana framework along with Oauth 2 to authenticate users. I could access SCIM REST Api's to create new users and their claims into WSO2. The claims were added under the dialect "urn:scim:schemas:core:1.0". I need to retrieve those claims while authenticating the user via Oauth 2. The service provider setup doesn't allow me to select claims from the dialect "urn:scim:schemas:core:1.0".

Is there an way I can retrieve the claims I have added to the user? The answer here suggest that we install an upgraded version of WSO2. Is that the only solution?

My Owin setup is (using NuGet package Owin.Security.Providers.WSO2):

```<code>app.UseWSO2Authentication(new WSO2AuthenticationOptions()
{
    BaseUrl = "https://identityserver:9443/",
    CallbackPath = new PathString("/Account/ExternalLoginConfirmation"),
    ClientId = "xxxxxxxxx",
    ClientSecret = "yyyyyyyyy"
});
</code>```

答:
You can retrieve a user's claims via the SCIM API.

Example of a get request :

curl -X GET -H "Authorization: Basic (base64 adminUsername:adminPassword)" "https://ishost:ismgtport/wso2/scim/Users?filter=userNameEquserstoredomain/userId"

```<code>{
  "schemas": [
    "urn:scim:schemas:core:1.0",
    "urn:scim:schemas:extension:wso2:1.0"
  ],
  "totalResults": 1,
  "Resources": [
    {
      "wso2Extension": {
        "nodeId": "2800",
        "employeeNumber": "TEST01"
      },
      "id": "e8a1791f-af81-4d61-ad33-51fd80c4d716",
      "roles": [
        "STORE_MANAGER"
      ],
      "name": {
        "familyName": "Manager",
        "givenName": "Mr"
      },
      "userName": "userstoredomain/userId",
      "phoneNumbers": [
        "0000000000"
      ],
      "externalId": "1_TEST01",
      "meta": {
        "lastModified": "2017-03-27T14:33:05",
        "created": "2017-03-27T14:33:05",
        "location": "https://localhost:9443/wso2/scim/Users/e8a1791f-af81-4d61-ad33-51fd80c4d716"
      }
    }
  ]
}
</code>```

https://docs.wso2.com/display/IS510/SCIM+APIs

The above example is retrieved via version 5.0.0

Body:
I am using WSO2 Identity Server as the identity provider for our websites. The websites are being developed in ASP.net and we are using in-built Owin-Katana framework along with Oauth 2 to authenticate users. I could access SCIM REST Api's to create new users and their claims into WSO2. The claims were added under the dialect "urn:scim:schemas:core:1.0". I need to retrieve those claims while authenticating the user via Oauth 2. The service provider setup doesn't allow me to select claims from the dialect "urn:scim:schemas:core:1.0".

Is there an way I can retrieve the claims I have added to the user? The answer here suggest that we install an upgraded version of WSO2. Is that the only solution?

My Owin setup is (using NuGet package Owin.Security.Providers.WSO2):

app.UseWSO2Authentication(new WSO2AuthenticationOptions()
{
    BaseUrl = "https://identityserver:9443/",
    CallbackPath = new PathString("/Account/ExternalLoginConfirmation"),
    ClientId = "xxxxxxxxx",
    ClientSecret = "yyyyyyyyy"
});

Body:
You can retrieve a user's claims via the SCIM API.

Example of a get request :

curl -X GET -H "Authorization: Basic (base64 adminUsername:adminPassword)" "https://ishost:ismgtport/wso2/scim/Users?filter=userNameEquserstoredomain/userId"

{
  "schemas": [
    "urn:scim:schemas:core:1.0",
    "urn:scim:schemas:extension:wso2:1.0"
  ],
  "totalResults": 1,
  "Resources": [
    {
      "wso2Extension": {
        "nodeId": "2800",
        "employeeNumber": "TEST01"
      },
      "id": "e8a1791f-af81-4d61-ad33-51fd80c4d716",
      "roles": [
        "STORE_MANAGER"
      ],
      "name": {
        "familyName": "Manager",
        "givenName": "Mr"
      },
      "userName": "userstoredomain/userId",
      "phoneNumbers": [
        "0000000000"
      ],
      "externalId": "1_TEST01",
      "meta": {
        "lastModified": "2017-03-27T14:33:05",
        "created": "2017-03-27T14:33:05",
        "location": "https://localhost:9443/wso2/scim/Users/e8a1791f-af81-4d61-ad33-51fd80c4d716"
      }
    }
  ]
}

https://docs.wso2.com/display/IS510/SCIM+APIs

The above example is retrieved via version 5.0.0


问:
How to loop through columns and calculate averages?
data series

I want to find the average for each segment of ten values down a column. (See data series picture)
Continuously all the way to the bottom of the data set. The data set can vary in length, and the code has to be "generic" of sorts.

Based on other code segments I have tried to do this:

```<code>Sub tenthavg()

Dim currentIndex As Long
Dim myArray() As Variant
Dim rng As Range

ReDim myArray(1 To 10)

Range("b1", Range("b1").End(xlDown)).Select
Set myArray = Selection
currentIndex = 1

Do Until currentIndex + 1 > UBound(myArray)
    ActiveSheet.Cells(currentIndex, "T") = AverageOfSubArray(myArray, currentIndex, 10)
    currentIndex = currentIndex + 1
Loop

End Sub

'=================================================================

Function AverageOfSubArray(myArray As Variant, startIndex As Long,   elementCount As Long) As Double
Dim runningTotal As Double
Dim i As Long

For i = startIndex To (startIndex + elementCount - 1)
    runningTotal = runningTotal + val(myArray(i))
Next i
AverageOfSubArray = runningTotal / elementCount
End Function
</code>```

Unfortunately I can't make it work. Am I approaching this the right way?

If so, what am I doing wrong?

答:
You can get your result in a simpler way:

```<code>Sub tenthavg()

Dim LastRow As Long
LastRow = ThisWorkbook.Sheets("Your Sheet Name").Columns(2).Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
Dim myArray(1 To 10) As Double

If LastRow < 10 Then
    MsgBox "There's not enough data!"
Else
    On Error Resume Next
    For x = 1 To LastRow - 9
        For y = 1 To 10
            myArray(y) = ThisWorkbook.Sheets("Your Sheet Name").Cells(y + x - 1, 2).Value
        Next y
        ThisWorkbook.Sheets("Your Sheet Name").Cells(x, 20).FormulaR1C1 = 0
        ThisWorkbook.Sheets("Your Sheet Name").Cells(x, 20).FormulaR1C1 = Application.Average(myArray)
    Next x
End If

End Sub
</code>```

Please note: I'm assuming you're data starts at B1 and you want the output on column T.

Body:
data series

I want to find the average for each segment of ten values down a column. (See data series picture)
Continuously all the way to the bottom of the data set. The data set can vary in length, and the code has to be "generic" of sorts.

Based on other code segments I have tried to do this:

Sub tenthavg()

Dim currentIndex As Long
Dim myArray() As Variant
Dim rng As Range

ReDim myArray(1 To 10)

Range("b1", Range("b1").End(xlDown)).Select
Set myArray = Selection
currentIndex = 1

Do Until currentIndex + 1 > UBound(myArray)
    ActiveSheet.Cells(currentIndex, "T") = AverageOfSubArray(myArray, currentIndex, 10)
    currentIndex = currentIndex + 1
Loop

End Sub

'=================================================================

Function AverageOfSubArray(myArray As Variant, startIndex As Long,   elementCount As Long) As Double
Dim runningTotal As Double
Dim i As Long

For i = startIndex To (startIndex + elementCount - 1)
    runningTotal = runningTotal + val(myArray(i))
Next i
AverageOfSubArray = runningTotal / elementCount
End Function

Unfortunately I can't make it work. Am I approaching this the right way?

If so, what am I doing wrong?

Body:
You can get your result in a simpler way:

Sub tenthavg()

Dim LastRow As Long
LastRow = ThisWorkbook.Sheets("Your Sheet Name").Columns(2).Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
Dim myArray(1 To 10) As Double

If LastRow < 10 Then
    MsgBox "There's not enough data!"
Else
    On Error Resume Next
    For x = 1 To LastRow - 9
        For y = 1 To 10
            myArray(y) = ThisWorkbook.Sheets("Your Sheet Name").Cells(y + x - 1, 2).Value
        Next y
        ThisWorkbook.Sheets("Your Sheet Name").Cells(x, 20).FormulaR1C1 = 0
        ThisWorkbook.Sheets("Your Sheet Name").Cells(x, 20).FormulaR1C1 = Application.Average(myArray)
    Next x
End If

End Sub

Please note: I'm assuming you're data starts at B1 and you want the output on column T.