Rectangle 27 1244

You can do that in one command:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

You can add -nodes if you don't want to protect your private key with a passphrase.

You can also add -nodes if you don't want to protect your private key with a passphrase, otherwise it will prompt you for "at least a 4 character" password. The days parameter (365) you can replace with any number to affect expiration date. It will then prompt you for things like "Country Name" but you can just hit enter and accept defaults.

Self-signed certs are not validated with any third party unless you import them to the browsers previously. If you need more security, you should use a certificate signed by a CA.

how do you do this without requiring a password on the cert? anytime i restart my web server i am required to provide a password. annoying!

Add -subj '/CN=localhost' to suppress questions about the contents of the certificate (replace localhost with your desired domain).

@the0ther read the sentence just below the command :)

How does signing with a 3rd-party provide more security?

-nodes

Does this support a wildcard?

-subj "/C=US/ST=Oregon/L=Portland/O=Company Name/OU=Org/CN=www.example.com"

Yes, but because is a self-signed certificado, doesn't make any sense usually :)

-sha256

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

ssl - How to create a self-signed certificate with openssl? - Stack Ov...

ssl openssl certificate ssl-certificate x509certificate
Rectangle 27 1195

You can do that in one command:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

You can also add -nodes if you don't want to protect your private key with a passphrase, otherwise it will prompt you for "at least a 4 character" password. The days parameter (365) you can replace with any number to affect expiration date. It will then prompt you for things like "Country Name" but you can just hit enter and accept defaults.

Self-signed certs are not validated with any third party unless you import them to the browsers previously. If you need more security, you should use a certificate signed by a CA.

Add -subj '/CN=localhost' to suppress questions about the contents of the certificate (replace localhost with your desired domain).

How does signing with a 3rd-party provide more security?

-nodes
-subj "/C=US/ST=Oregon/L=Portland/O=Company Name/OU=Org/CN=www.example.com"
-sha256

ssl - How to create a self-signed certificate with openssl? - Stack Ov...

ssl openssl certificate ssl-certificate x509certificate
Rectangle 27 15

You can do it in one of the following ways:

from itertools import chain
#compute the list dynamically here:
my_obj_list = list(obj1, obj2, ...)
#and then 
none_qs = MyModel.objects.none()
qs = list(chain(none_qs, my_obj_list))
none_qs = MyModel.objects.none()
qs = none_qs | sub_qs_1 | sub_qs_2

However, This would not work for sliced querysets

dynamically create a list, and then append it at once.

actually, you dont need the none qs. You could simply create a list and pass it in the context.

Does not work. type(qs) = list

@dz210 what are you trying to do? = is assignment, == is equality check.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

how to create an empty queryset and to add objects manually in django ...

django django-queryset
Rectangle 27 13

You can do this in one go with

git branch --merged master | grep -v master | xargs -n 1 git push --delete origin

Dump that in a script called 'clean' if you find you're doing this often.

git branch -a --merged remotes/origin/master | grep -v master | grep "remotes/origin/" | cut -d "/" -f 3 | xargs -n 1 git push --delete origin
remotes/origin/master
master

@skalee thanks! this worked for me except when branches are in folders - use cut -d "/" -f 3- instead. I also had to add a grep -v develop to stop it trying to remove my dev branch.

xargs -n 1 is too slow, if you have many branches use xargs -n 20 instead.

git branch - How to delete all remote git branches which have already ...

git git-branch
Rectangle 27 13

You can do this in one go with

git branch --merged master | grep -v master | xargs -n 1 git push --delete origin

Dump that in a script called 'clean' if you find you're doing this often.

git branch -a --merged remotes/origin/master | grep -v master | grep "remotes/origin/" | cut -d "/" -f 3 | xargs -n 1 git push --delete origin
remotes/origin/master
master

@skalee thanks! this worked for me except when branches are in folders - use cut -d "/" -f 3- instead. I also had to add a grep -v develop to stop it trying to remove my dev branch.

xargs -n 1 is too slow, if you have many branches use xargs -n 20 instead.

git branch - How to delete all remote git branches which have already ...

git git-branch
Rectangle 27 22

Since Java 8, you can do that in one line:

Files.list(Paths.get("your/path/here")).count();

Regarding the 5000 child nodes and inode aspects:

This method will iterate over the entries but as Varkhan suggested you probably can't do better besides playing with JNI or direct system commands calls, but even then, you can never be sure these methods don't do the same thing!

Files.list
Iterable
Files.newDirectoryStream
FileSystemProvider.newDirectoryStream
sun.nio.fs.UnixFileSystemProvider.class
sun.nio.fs.UnixSecureDirectoryStream

So, there is an iterator that will loop through the entries here.

The actual count is performed by the count/sum reducing API exposed by Java 8 streams. In theory, this API can perform parallel operations without much effort (with multihtreading). However the stream is created with parallelism disabled so it's a no go...

The good side of this approach is that it won't load the array in memory as the entries will be counted by an iterator as they are read by the underlying (Filesystem) API.

Finally, for the information, conceptually in a filesystem, a directory node is not required to hold the number of the files that it contains, it can just contain the list of it's child nodes (list of inodes). I'm not an expert on filesystems, but I believe that UNIX filesystems work just like that. So you can't assume there is a way to have this information directly (i.e: there can always be some list of child nodes hidden somewhere).

The Java 8 Files.list() throws IOException; the File class's list() method doesn't throw any exceptions.

I've been using Files.list() for a directory with 1-2 million files, and of course it takes a while. But I have a feeling that this is behind a few GC overhead exceptions I've encountered, because millions of file objects get instantiated and destroyed for each call. Still looking for a performant and memory-safe method...

performance - Counting the number of files in a directory using Java -...

java performance file directory
Rectangle 27 115

You can do both in one query using the OVER clause on another COUNT

select
    count(*) RecordsPerGroup,
    COUNT(*) OVER () AS TotalRecords
from temptable
group by column_1, column_2, column_3, column_4

This down side of this solution is that it gives you the answer multiple times (for each combination of column_1, column_2, column_3, column_4). This may or may not be a significant side-effect, depending on how you process the results.

To return just the number of records, add a select top(1) count(*) over () as ....

sql server - Count number of records returned by group by - Stack Over...

sql-server tsql count group-by
Rectangle 27 287

Yes, you can do this in one line (though for robust IOException handling you wouldn't want to).

String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
System.out.println(content);

This uses a java.util.Scanner, telling it to delimit the input with \Z, which is the end of the string anchor. This ultimately makes the input have one actual token, which is the entire file, so it can be read with one call to next().

There is a constructor that takes a File and a String charSetName (among many other overloads). These two constructor may throw FileNotFoundException, but like all Scanner methods, no IOException can be thrown beyond these constructors.

You can query the Scanner itself through the ioException() method if an IOException occurred or not. You may also want to explicitly close() the Scanner after you read the content, so perhaps storing the Scanner reference in a local variable is best.

For completeness, these are some really good options if you have these very reputable and highly useful third party libraries:

com.google.common.io.Files contains many useful methods. The pertinent ones here are:

String toString(File, Charset)
  • Using the given character set, reads all characters from a file into a String
List<String> readLines(File, Charset)
  • ... reads all of the lines from a file into a List<String>, one entry per line
org.apache.commons.io.IOUtils
String toString(InputStream, String encoding)
  • Using the specified character encoding, gets the contents of an InputStream as a String
List readLines(InputStream, String encoding)
  • ... as a (raw) List of String, one entry per line

Alright! Though may not be robust as you mentioned, no doubt this does it in minimum lines of code !

Please also see solution below for Java 7 mechanism that's essentially one line with the default API, as with many things Java has moved on slightly since this question.

Unfortunately, the Scanner solution fails with empty files (NoSuchElementException)

In addition to failing on empty files, it also omits the newline at the end of the file, if it exists.

return new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next(); is better as it does not fail on empty files.

java - What is simplest way to read a file into String? - Stack Overfl...

java file file-io
Rectangle 27 287

Yes, you can do this in one line (though for robust IOException handling you wouldn't want to).

String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
System.out.println(content);

This uses a java.util.Scanner, telling it to delimit the input with \Z, which is the end of the string anchor. This ultimately makes the input have one actual token, which is the entire file, so it can be read with one call to next().

There is a constructor that takes a File and a String charSetName (among many other overloads). These two constructor may throw FileNotFoundException, but like all Scanner methods, no IOException can be thrown beyond these constructors.

You can query the Scanner itself through the ioException() method if an IOException occurred or not. You may also want to explicitly close() the Scanner after you read the content, so perhaps storing the Scanner reference in a local variable is best.

For completeness, these are some really good options if you have these very reputable and highly useful third party libraries:

com.google.common.io.Files contains many useful methods. The pertinent ones here are:

String toString(File, Charset)
  • Using the given character set, reads all characters from a file into a String
List<String> readLines(File, Charset)
  • ... reads all of the lines from a file into a List<String>, one entry per line
org.apache.commons.io.IOUtils
String toString(InputStream, String encoding)
  • Using the specified character encoding, gets the contents of an InputStream as a String
List readLines(InputStream, String encoding)
  • ... as a (raw) List of String, one entry per line

Alright! Though may not be robust as you mentioned, no doubt this does it in minimum lines of code !

Please also see solution below for Java 7 mechanism that's essentially one line with the default API, as with many things Java has moved on slightly since this question.

Unfortunately, the Scanner solution fails with empty files (NoSuchElementException)

In addition to failing on empty files, it also omits the newline at the end of the file, if it exists.

return new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next(); is better as it does not fail on empty files.

I have used this for some time, but it turns out it doesn't always work! Sometimes \\Z will actually occur in the file and will cause this to fail.

java - What is simplest way to read a file into String? - Stack Overfl...

java file file-io
Rectangle 27 286

Yes, you can do this in one line (though for robust IOException handling you wouldn't want to).

String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
System.out.println(content);

This uses a java.util.Scanner, telling it to delimit the input with \Z, which is the end of the string anchor. This ultimately makes the input have one actual token, which is the entire file, so it can be read with one call to next().

There is a constructor that takes a File and a String charSetName (among many other overloads). These two constructor may throw FileNotFoundException, but like all Scanner methods, no IOException can be thrown beyond these constructors.

You can query the Scanner itself through the ioException() method if an IOException occurred or not. You may also want to explicitly close() the Scanner after you read the content, so perhaps storing the Scanner reference in a local variable is best.

For completeness, these are some really good options if you have these very reputable and highly useful third party libraries:

com.google.common.io.Files contains many useful methods. The pertinent ones here are:

String toString(File, Charset)
  • Using the given character set, reads all characters from a file into a String
List<String> readLines(File, Charset)
  • ... reads all of the lines from a file into a List<String>, one entry per line
org.apache.commons.io.IOUtils
String toString(InputStream, String encoding)
  • Using the specified character encoding, gets the contents of an InputStream as a String
List readLines(InputStream, String encoding)
  • ... as a (raw) List of String, one entry per line

Alright! Though may not be robust as you mentioned, no doubt this does it in minimum lines of code !

Please also see solution below for Java 7 mechanism that's essentially one line with the default API, as with many things Java has moved on slightly since this question.

Unfortunately, the Scanner solution fails with empty files (NoSuchElementException)

In addition to failing on empty files, it also omits the newline at the end of the file, if it exists.

return new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next(); is better as it does not fail on empty files.

java - What is simplest way to read a file into String? - Stack Overfl...

java file file-io
Rectangle 27 286

Yes, you can do this in one line (though for robust IOException handling you wouldn't want to).

String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
System.out.println(content);

This uses a java.util.Scanner, telling it to delimit the input with \Z, which is the end of the string anchor. This ultimately makes the input have one actual token, which is the entire file, so it can be read with one call to next().

There is a constructor that takes a File and a String charSetName (among many other overloads). These two constructor may throw FileNotFoundException, but like all Scanner methods, no IOException can be thrown beyond these constructors.

You can query the Scanner itself through the ioException() method if an IOException occurred or not. You may also want to explicitly close() the Scanner after you read the content, so perhaps storing the Scanner reference in a local variable is best.

For completeness, these are some really good options if you have these very reputable and highly useful third party libraries:

com.google.common.io.Files contains many useful methods. The pertinent ones here are:

String toString(File, Charset)
  • Using the given character set, reads all characters from a file into a String
List<String> readLines(File, Charset)
  • ... reads all of the lines from a file into a List<String>, one entry per line
org.apache.commons.io.IOUtils
String toString(InputStream, String encoding)
  • Using the specified character encoding, gets the contents of an InputStream as a String
List readLines(InputStream, String encoding)
  • ... as a (raw) List of String, one entry per line

Alright! Though may not be robust as you mentioned, no doubt this does it in minimum lines of code !

Please also see solution below for Java 7 mechanism that's essentially one line with the default API, as with many things Java has moved on slightly since this question.

Unfortunately, the Scanner solution fails with empty files (NoSuchElementException)

In addition to failing on empty files, it also omits the newline at the end of the file, if it exists.

return new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next(); is better as it does not fail on empty files.

java - What is simplest way to read a file into String? - Stack Overfl...

java file file-io
Rectangle 27 12

You can do it in one line of code:

Log.d("intent URI", intent.toUri(0));

At the end of this string (the part that I bolded) you can find the list of extras (only one extra in this example).

This is according to the toUri documentation: "The URI contains the Intent's data as the base URI, with an additional fragment describing the action, categories, type, flags, package, component, and extras."

If you just want to debug and see what the contents of the intent are, this is the best option. Thank you very much

android - Listing all extras of an Intent - Stack Overflow

android android-intent
Rectangle 27 98

@Russ Cam: Sorry, but besides the fact that this sounds awful, the entity the table stores is a "CustomerAddress". The fact that there can be more than one of them is inherent, no need to reflect it in the table name.

Plural: Customers.. CustomerAddresses, CustomerAddressAuditTrails.. you wouldn't plural every word, just the object noun

sql - Singular or plural database table names? - Stack Overflow

sql mysql sql-server
Rectangle 27 98

@Russ Cam: Sorry, but besides the fact that this sounds awful, the entity the table stores is a "CustomerAddress". The fact that there can be more than one of them is inherent, no need to reflect it in the table name.

Plural: Customers.. CustomerAddresses, CustomerAddressAuditTrails.. you wouldn't plural every word, just the object noun

sql - Singular or plural database table names? - Stack Overflow

sql mysql sql-server
Rectangle 27 30

Here are the ones I can find:

Is there a .NET/C# wrapper for SQLite? - Stack Overflow

c# .net database sqlite
Rectangle 27 24

If I create a global variable in one function, how can I use that vari...

We can create a global with the following function:

def create_global_variable():
    global global_variable # must declare it to be a global first
    # modifications are thus reflected on the module's global scope
    global_variable = 'Foo'

Writing a function does not actually run its code. So we call the create_global_variable function:

>>> create_global_variable()

You can just use it, so long as you don't expect to change which object it points to:

def use_global_variable():
    return global_variable + '!!!'

and now we can use the global variable:

To point the global variable at a different object, you are required to use the global keyword again:

def change_global_variable():
    global global_variable
    global_variable = 'Bar'

Note that after writing this function, the code actually changing it has still not run:

>>> use_global_variable()
'Foo!!!'

So after calling the function:

>>> change_global_variable()

we can see that the global variable has been changed. The global_variable name now points to 'Bar':

Note that "global" in Python is not truly global - it's only global to the module level. So it is only available to functions written in the modules in which it is global. Functions remember the module in which they are written, so when they are exported into other modules, they still look in the module in which they were created to find global variables.

If you create a local variable with the same name, it will overshadow a global variable:

def use_local_with_same_name_as_global():
    # bad name for a local variable, though.
    global_variable = 'Baz' 
    return global_variable + '!!!'

>>> use_local_with_same_name_as_global()
'Baz!!!'

But using that misnamed local variable does not change the global variable:

>>> use_global_variable()
'Bar!!!'

Note that you should avoid using the local variables with the same names as globals unless you know precisely what you are doing and have a very good reason to do so. I have not yet encountered such a reason.

python - Using global variables in a function other than the one that ...

python global-variables scope
Rectangle 27 24

If I create a global variable in one function, how can I use that vari...

We can create a global with the following function:

def create_global_variable():
    global global_variable # must declare it to be a global first
    # modifications are thus reflected on the module's global scope
    global_variable = 'Foo'

Writing a function does not actually run its code. So we call the create_global_variable function:

>>> create_global_variable()

You can just use it, so long as you don't expect to change which object it points to:

def use_global_variable():
    return global_variable + '!!!'

and now we can use the global variable:

To point the global variable at a different object, you are required to use the global keyword again:

def change_global_variable():
    global global_variable
    global_variable = 'Bar'

Note that after writing this function, the code actually changing it has still not run:

>>> use_global_variable()
'Foo!!!'

So after calling the function:

>>> change_global_variable()

we can see that the global variable has been changed. The global_variable name now points to 'Bar':

Note that "global" in Python is not truly global - it's only global to the module level. So it is only available to functions written in the modules in which it is global. Functions remember the module in which they are written, so when they are exported into other modules, they still look in the module in which they were created to find global variables.

If you create a local variable with the same name, it will overshadow a global variable:

def use_local_with_same_name_as_global():
    # bad name for a local variable, though.
    global_variable = 'Baz' 
    return global_variable + '!!!'

>>> use_local_with_same_name_as_global()
'Baz!!!'

But using that misnamed local variable does not change the global variable:

>>> use_global_variable()
'Bar!!!'

Note that you should avoid using the local variables with the same names as globals unless you know precisely what you are doing and have a very good reason to do so. I have not yet encountered such a reason.

python - Using global variables in a function other than the one that ...

python global-variables scope
Rectangle 27 11

As explained in Tom's answer, you can not use more than one @Path annotation on a single method, because you will run into error: duplicate annotation at compile time.

@Path("{foo}")
public Response rest(@PathParam("foo") final String foo) {
    return this.rest(foo, "");
}

@Path("{foo}/{bar}")
public Response rest(@PathParam("foo") final String foo,
                     @PathParam("bar") final String bar) {
    return Response.ok(foo + " " + bar).build();
}

You could also use more different method names if you run into the case where multiple overloaded methods have the signature.

java - Can we have more than one @Path annotation for same REST method...

java rest jersey jax-rs
Rectangle 27 9

Or you can do it in one line.

redirect_to check_in_path, flash: {notice: "Successfully checked in"}

@JayEl-Kaake what ruby version are you using?

It was a while ago, so I'm not sure... I'll redact my comment since it looks like that should work.

path, flash: {notice: ... seems like Rails 3, while path, alert: ... is Rails 4+

How to display a Rails flash notice upon redirect? - Stack Overflow

ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 ruby-on-rails-4 rails-flash
Rectangle 27 8

You can do this in one line of code:

new FileManager().download_file('http://url','target_path',Log('downloaded success'));

target_path: can include directory (example: dira/dirb/file.html) and the directories will be created recursively.

You can find the library to do this here:

Download files and store them locally with Phonegap/jQuery Mobile Andr...

android ios cordova jquery-mobile