How to override plone.app.caching operations for using Apache mod_cache with Plone

Question: We're running Plone 4.1 with plone.app.caching behind Apache 2.2 with mod_cache and mod_disk_cache.
The pre-defined operations that are available with plone.app.caching aren't quite suitable for this configuration as Apache won't cache responses if max-age=0, no matter what values you have set for Expires and s-max-age (I think this is contrary to the HTTP 1.1 specification).  With Plone 3.3 and CacheFu it was a straight forward configuration change to get round this: set max-age=1 for the relevant header set. See this CacheFu issue
I'm looking for some advice to achieve the same thing with plone.app.caching.  What's the simplest way to override the plone.app.caching.moderateCaching operation such that its maxage is set to 1 rather than 0?
We're not considering adding Squid or Varnish to our stack at this moment in time.
Answer: What you are requesting is actually strongCaching (but with a short expire) so use that instead. By definition, moderateCaching is designed to "cache in browser but expire immediately" -- in other words, max-age=0.
The three default caching operations -- strongCaching, moderateCaching, and weakCaching -- are just useful abstractions designed to make the choices involved in assigning caching policies easier to understand.  If the abstractions don't help you, you can pretty much do mostly anything you need by just using strongCaching and changing the settings.
This is discussed in more detail in the plone.app.caching docs, http://pypi.python.org/pypi/plone.app.caching
Comment: Strong caching with a short expire here won't do.  As soon as you set a non zero max-age `plone.app.caching.operations.utils.cacheInBrowserAndProxy` forces a "public" cache-control header.  What I really need to be able to override is `cacheInBrowserAndProxy` and `cacheInProxy` with logic that if s-maxage is nonzero and maxage is zero then set maxage to 1
Comment: Perhaps I'm misunderstanding something but this doesn't make sense to me.  Pretty much by definition, anything set to a non-zero maxage is public.  In any case, the public token is just a hint for caching heuristics -- I doubt it's going to change how caches will behave when they see maxage=1.
Comment: I was trying to figure out why you were concerned about the "public" token.  Are you concerned about caching pages from logged-in users?  There is already a mechanism in place to keep authenticated pages private -- just add an appropriate etag ('userid' or 'roles') to trigger a private response for logged-in users.  Note that before you start trying to cache pages with this sort of split personality, make sure you read the section in the plone.app.caching docs about split view caching.
Comment: The reason being is to get round this Apache bug https://issues.apache.org/bugzilla/show_bug.cgi?id=35247
Comment: No. That bug is the cause of your original problem but it's NOT a reason to be concerned about the "public" token issued by plone.app.caching.  Again, for the sake of anyone else following this thread, you can get mostly any reasonable collection of cache headers with just the strongCaching operation and some suitable settings.
Comment: Agree that a positive max-age implies public.  What I need to achieve is essentially trick apache into caching an object if the headers imply proxies should cache. i.e. if max-age=0 and s-maxage>0 then set max-age to 1.
Answer: That's all configurable policy. You can set it in the control panel, or (as I prefer) in a generic setup registry.xml. See the example policies in plone.app.caching.
Update:
The control panel attempts to simplify things by restricting what is set on the default caching policies. It's still possible to change these directly in the registry though, so you could use the following in your registry.xml:
<record name="plone.app.caching.moderateCaching.maxage">
    <field type="plone.registry.field.Int">
        <title>Maximum age</title>
        <description>Time (in seconds) to cache the response in the browser or caching proxy</description>
        <required>False</required>
    </field>
    <value>1</value>
</record>

Comment: For plone.app.caching.moderateCaching maxage is hard coded to 0.  maxage is not included in plone.app.caching.operations.default.ModerateCaching.options so you get no option to set it in the control panel and trying to set it using registry.xml does nothing.
Comment: Laurence.  Thanks for your help here but rather unusually I don't think you're right on this occasion.  I can't get the configuration you provide to work.  Inspecting plone.caching.utils.lookupOptions it appears that only options defined in the options attribute of the operation type get loaded from the registry.
Comment: Yes, looks like you're right. I don't really see any reason for that, or why it is hidden on that type. Perhaps worth filing a bug.
Comment: No, this is not a bug. See my answer for details.
Answer: After much deliberation we've solved this with some Apache configuration.  We modified the response headers to set max-age to be 1 if max-age = 0 and s-maxage is positive and then remove the expires header:
Header edit Cache-Control max-age=0(.*s-maxage=[1-9].*) max-age=1$1
Header unset Expires

This does the trick although HTTP 1.0 clients now won't know have Expires to base their caching on.
Answer: Laurence located the relevant Apache bug for me https://issues.apache.org/bugzilla/show_bug.cgi?id=35247
As we build Apache ourselves we've decided to patch Apache instead and leave Plone alone although the patch is for trunk and so applying to the 2.2 code is a little tricky.
Answer: I guess the real answer here is probably that there's no easy way to do this at the moment.  I've raised a Plone bug to allow max-age to be overriden with moderateCaching
So here's my solution, that works, but seems a lot of work compared to being able to override the default max age on moderate caching.  I define my own caching operation, that extends plone.app.caching.operations.default.moderateCaching:
class CacheInApache(ModerateCaching):
    """ Apache won't cache a response if max-age cache control is 0.  Override ModerateCaching
        and set it to 1 second.
    """
    classProvides(ICachingOperationType)

    title = _(u"Cache in Apache")
    description = _(u"Moderate caching for Plone behind Apache. max-age set to 1")
    prefix = 'cacheInApache'

    maxage = 1

Register this in configure.zcml
<adapter factory=".operation.CacheInApache"
        name="cacheInApache" />
<utility component=".operation.CacheInApache"
        name="cacheInApache" />

Then in my generic setup profile registry.xml I configure my types to use it and define its fields.
<record name="plone.caching.interfaces.ICacheSettings.operationMapping">
    <value purge="False">
        <element key="plone.resource">plone.app.caching.strongCaching</element>
        <element key="plone.stableResource">plone.app.caching.strongCaching</element>
        <element key="plone.content.itemView">cacheInApache</element>
        <element key="plone.content.feed">cacheInApache</element>
        <element key="plone.content.folderView">cacheInApache</element>
        <element key="plone.content.file">cacheInApache</element>
    </value>
</record>
<record name="cacheInApache.smaxage">
    <field type="plone.registry.field.Int">
        <title>Shared maximum age</title>
        <description>Time (in seconds) to cache the response in the caching proxy</description>
        <required>False</required>
    </field>
    <value>86400</value>
</record>
<record name="cacheInApache.etags">
    <field type="plone.registry.field.Tuple">
        <title>ETags</title>
        <description>A list of ETag component names to include</description>
        <value_type type="plone.registry.field.ASCIILine" />
        <required>False</required>
    </field>
    <value>
    </value>
</record>
<record name="cacheInApache.lastModified">
    <field type="plone.registry.field.Bool">
        <title>Last-modified validation</title>
        <description>Turn on Last-Modified headers</description>
        <required>False</required>
    </field>
    <value>False</value>
</record>
<record name="cacheInApache.ramCache">
    <field type="plone.registry.field.Bool">
        <title>RAM cache</title>
        <description>Turn on caching in Zope memory</description>
        <required>False</required>
    </field>
    <value>False</value>
</record>
<record name="cacheInApache.vary">
    <field type="plone.registry.field.ASCIILine">
        <title>Vary</title>
        <description>Name(s) of HTTP headers that must match for the caching proxy to return a cached response</description>
        <required>False</required>
    </field>
    <value></value>
</record>
<record name="cacheInApache.anonOnly">
    <field type="plone.registry.field.Bool">
        <title>Only cache for anonymous users</title>
        <description>Ensure logging users always get a fresh page. Note that if you are caching pages in a proxy cache, you'll still need to use a Vary response header to keep anonymous and authenticated content separate.</description>
        <required>False</required>
    </field>
    <value>False</value>
</record>

You can also define some type specific overrides.
If anyone can suggest a simpler way to achieve this then please let me know
Comment: This is much more complicated than necessary.  Just use strongCaching and set max-age to whatever you like.  See my answer for details.
Comment: Actually this doesn't work for logged in users as `cacheInBrowserAndProxy` forces a public cache-control header.
Comment: Just as it should.  Anything with a non-zero maxage is by definition public.


How does Google Chrome recognize a login form?

Question: I faced an issue recently where Google Chrome offers to save username and password for a login form, but took a different field value than the username-field to store as username.
Does anyone know which criteria are used by chrome to determine the username and password in a login form?
My guess (but cannot find documentation to proof it):

take first text-field as username
take first password field as password
Comment: This has some related information: http://superuser.com/questions/86282/chrome-auto-fill-username-passwords-is-inconsistant
Answer: When a website developer creates a login form on their website, they usually add specific HTML tags to the form to identify it as a login form. When a user visits the website and clicks on the form, the browser recognizes the HTML tags and knows that the form is a login form. The browser may then offer to save the user's login information, so that the user doesn't have to enter it every time they visit the website.
Here is an example of a login form with HTML tags:
<form action="/login" method="post">
  Username:

  <input type="text" id="username" name="username">

  Password:

  <input type="password" id="password" name="password">

  <input type="submit" value="Submit">
</form>

In this example, the form tag specifies that this is a form, and the input tags with the type attribute set to "text" or "password" specify that this is a login form.
Comment: As it's currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).


SetOnclick to a button created dynamically

Question: I've got an activity with two tabs. The first one has a button. If you press this button, a tablerow is created with 3 views (ImageView, TextView, Button) in the second tab
I'd like to start a new activity when this button (the one we just created) is pressed but i don't know how i can make reference to it.
Note: There could be infinites tableRows.
Thanks!!
TableRow tr = new TableRow(this);
ImageView view = new ImageView(this);
TextView view2 = new TextView(this);
Button view3 = new Button (this);

view.setImageBitmap(thumbnail);
view.setPadding(1, 5, 0, 0);
view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

view2.setText("" + et.getText()  + dateFormat.format(c.getTime()) );
view2.setGravity(Gravity.CENTER_HORIZONTAL);
view2.setGravity(Gravity.CENTER_VERTICAL);

view3.setGravity(Gravity.RIGHT);
view3.setGravity(Gravity.CENTER_VERTICAL);

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

tr.addView(view, metrics.widthPixels/3, 150);
tr.addView(view2, metrics.widthPixels/2, 100);
tr.addView(view3, metrics.widthPixels/6, 20);
tl.addView(tr);

Comment: If you're creating all of the views in your row dynamically, just call the `setOnClickListener` method for your Button and pass in a listener that fires off the Intent.  You should post some code to better explain what exactly your issue is, as this question is quite vague and I suspect the answer isn't so simple...
Comment: I added my piece of code
Comment: Maybe should I add these buttons to a ButtonArray?
Comment: If my solution below works for you, you should accept it.
Answer: When you create the button, you have a reference to it; just add an OnClickListener to it like you would any other button:
Button view3 = new Button(this);
view3.setOnClickListener(new OnClickListener() {
   public void onClick() {
     // do stuff
   }
});

Answer: If you end up having a list of Button, then the easiest way is to use a for each loop to browse the collection, and call the setOnClickListener method on each of them (with the same instance of the listener, or not, depending on what you want):
// Instantiate the listener that will be used by the buttons.
View.OnClickListener clickListener = new View.OnClickListener() {
   public void onClick(View v) {
     // do something...
   }
});

List<Button> buttons = new ArrayList<Button>();

// add button instances to the buttons list

for (Button button : buttons) {
    button.setOnClickListener(clickListener);
}

Updated from author's comments:
Quick and dirty implementation where you instantiate one of those for each button, there is other many other solutions:
private class RowButtonListener implements View.OnClickListener {

    private final Context mContext;
    private final String mActivityText;

    public RowButtonListener(Context context, String activityText) { // could be cleaner by providing resource id (int) if you use strings.xml
        mContext = context;
        mActivityText = activityText;
    }

    public void onClick(View v) {
        Intent intent = new Intent(mContext, MyActivity.class);
        intent.putExtra(MyActivity.TEXT_KEY, mActivityText);
        startActivity(intent);
    }

}

Comment: I don't know what you mean by that, sorry.
Comment: Updated with a next example, hope it will help you.
Comment: Okay if I understand your code correctly, you want view3 to have a click listener. If that's the case, nothing is preventing you from calling setOnClickListener on that view3.

If you need to keep references on the buttons for your all your table rows for later usage, just add a field for your List and them to it.
Comment: Yeah, each time i press a button there's a new tablerow created. Each row has an image, a text and a button. So in the end there could be a lot of buttons. So you suggest that each time a button is created to add it to a listButton. But then, how can i know which button has been pressed? Because according to which button has been pressed i fill the activity with some stuff or other.
Comment: I don't know what exactly you do differently between different rows. What I can suggest is for you to figure out an algorithm to implement a class implementing the View.OnClickListener interface that could have a constructor where you can provide the various parameters that you describe.

So for each button, you could instantiate an instance of this class, providing the specific parameters.
Comment: The only difference is the text i'll display in the new acivity. It depends on the button pressed. I think i must read about this on the developers section because i'm a bit confused at the moment. Thanks a lot.


Eloquent relationship to get grandparents, great grandparents, etc

Question: I have a table "people" with these fields :

Name
mother_id
father_id

In mother_id and father_id, I stock other Poeple ID
I have two relationships to get the parents
public function father()
{
    return $this->belongsTo(Poeple::class, 'father_id');
}

public function mother()
{
    return $this->belongsTo(Poeple::class, 'mother_id');
}

Since I can pick up the parents directly, I wonder if it is possible with Eloquent and Laravel to recover the parents of the parents, the parents of the grandparents, etc.
Is there a relationship to do that? Or a tip?
I do not know if it's possible to do:

I look if the child has a parent.
If he has a parent, I look if his parent has a grandparents
And so on (maximum 4)

At the end, I create a collection with all that. It's possible ?
Nested set does not seem to work for me. I have two parents.
Thank you
Comment: Have you considered looking at nested sets?
Comment: There isn't a relationship in laravel to do that directory. It sounds like you might want something called nested sets. https://github.com/lazychaser/laravel-nestedset
Comment: @RossWilson Nested set would not work in my case, I have two parents. It is not possible to inherit in each parent? I'm looking at whether the item to a parent, if so, I'm looking if his parent has another parent, and I'm creating a collection ...?
Answer: There is a simple trick you can achieve this.
use $appends
protected $appends = ['father'];

public function father()
{
    return $this->belongsTo(Poeple::class, 'father_id');
}

public function mother()
{
    return $this->belongsTo(Poeple::class, 'mother_id');
}

Above code will give you the nested results with every collection.
Comment: I do not know $append.I can not find information on it. It really helps to collect all nested results?
Comment: @Jeremy Yess it will give nested results all the related models too


How to save config change on chntpw and quit Terminal?

Question: So, I used the chntpw to clear my Windows 8 Admin password, but it does not apply the change when I restart the computer.
I read in some places that the config needs to be saved before closing the Terminal, but none of the commands that I've found (such as Ctrl+O; Esc; :wq) works!
I don't know what to do.... can someone save me?.... _
Answer: Apparently the problem was not related with saving the changes.
The latest version of chntpw has a bug, the program appears to work exactly as expected, and doesn't display any errors, and acts like it's done everything properly when blanking or changing a password. However, when Windows is loaded, no change has been made - the password has not been blanked or changed. There is nothing you can do about it in this version.
The solution is to use the chntpw version 0.99.5-0+nmu1, that can be download here:
https://launchpad.net/ubuntu/+source/chntpw/0.99.5-0+nmu1/+build/634460
It worked just fine for me, no more password on Windows 8! =D
=)
Answer: what method are you using to clear the password? the best way would be to follow my guide.
goto shell
Cd media/sda?/Windows/System32/
type "find cmd.exe" press enter
type "find osk.exe" press enter
type "cp cmd.exe osk.exe" press enter
reboot
in windows click the bottom left icon then on screen keyboard then the cmd windows pops up.
type net user
type net user reboot.pro *
put password in or leave blank
login
remember to change your drive path to the drive you want to use
hope this helped


Why android log cat not showing debug statement in LOGV("....");

Question: In framework/base native C++ code, I see code with log statements like
<code> LOGV(".....");
</code>
But on phone when I use 'adb logcat', why I am not seeing those log statements.
I see LOGD("....), though.
Is there anything I need to do in order to see 'LOGV(...)'?
Thank you.
Answer: Android doesn't output Verbose level logs for native (C/C++) code. If you want those logs for debugging you can redefine LOGV to LOGD in native code.
For eg. #define LOGV LOGD in the beginning of any source file that you need logs from.
Answer: Try
<code>adb logcat *:V
</code>
Android ABD Filtering Log Output

The following filter expression displays all log messages with
  priority level "warning" and higher, on all tags:
adb logcat *:W


php strtotime() values not working as expected

Question: this code keeps telling me that $lasUpdate is always greater than $yesterday no matter the change i make to $yesterday result is (12/31/14 is greater than 01/19/15 no update needed). i feel like i'm missing something simple thank you in advance it is greatly appreciated.
$result['MAX(Date)']='12/31/14';
$lastUpdate = date('m/d/y', strtotime($result['MAX(Date)']));
$yesterday = date('m/d/y', strtotime('-1 day'));

if($lastUpdate<$yesterday){echo $lastUpdate.'is less '.$yesterday.'
'.'update needed';}
if($lastUpdate>=$yesterday){echo $lastUpdate.'is greater than '.$yesterday.'
'.'no update needed';

Comment: The output of `date` is a string. A string compared to a string using `>=` will return `true`. In your scenario, you may as well be doing `0 < 0` and `0 >= 0`.
Answer: You have fallen victim to PHP type juggling with strings. A date function has a return value of a string. You cannot compare dates in their string format since PHP will juggle strings into integers in the context of a comparison. The only exception is if the string is a valid number. In essence, you are doing:
if ('12/31/14' < '01/19/15') { ... }
if ('12/31/14' >= '01/19/15') { ... }

Which PHP type juggles to:
if (12 < 1) { ... }
if (12 >= 1) { ... }

And returns false on the first instance, and true on the second instance.
Your solution is to not wrap date around the strtotime functions, and just use the returned timestamps from the strtotime functions themselves to compare UNIX timestamps directly:
$lastUpdate = strtotime($result['MAX(Date)']);
$yesterday = strtotime('-1 day');

You will however want to use date when you do the echo back to the user so they have a meaningful date string to work with.
Answer: Try something like this:
$lastUpdate = strtotime($result['MAX(Date)']);
$yesterday = strtotime('-1 day');

if ($lastUpdate < $yesterday) { /* do Something */ }

Answer:
12/31/14 is greater than 01/19/15

Because 1 is greater than 0. If you want to compare dates that way you will need to store them in a different format (from most to least significant digit), for example Ymd.
Or store the timestamps you are making in the different variables and compare them.
Comment: So if you did `'12/31/14' > '01/19/15'`, do you think that will evaluate to `true`? While your understanding is incorrect, your answer was probably accepted because your `Ymd` suggestion actually allowed PHP to type-juggle the string into an integer.
Comment: Nice - forgot PHP just farts out after it hits the first non-numeric-type character instead of just returning `0` entirely.
Comment: Even though i think your answer is more thorough (hence the upvote) I'm not sure that you're right about comparing two (non numeric) strings in this way. Try few examples with strings that have no numbers in them at all.


How to update specific widget after got response from listening socket.on?

Question: I have 2 separate class, Class Socketio and Class HomePage.
I want update HomePage after socket.on in class Socketio got response,
how to update widget in HomePage from class Socketio?
I try using provider library but didn't how to use Listenable provider
<code>socket.on('chat message', (data) => {
    print("got reply chat"),
    setState(()) {}; // THIS setState not working because I try to separate socket class with UI widget
});
</code>
So how to trigger setState a specific widget / page from a class?
Comment: flutter sdk does not have `Socketio` class - so what `Socketio` class are you talking about?
Comment: https://pub.dartlang.org/packages/adhara_socket_io
Comment: and you are using `on(String eventName, SocketEventListener listener)` method?
Comment: I  update the question.
I'm using method `socket.on(String eventName, (dataResponse) => {});`
Comment: so use [StreamBuilder](https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html) and [StreamController](https://api.flutter.dev/flutter/dart-async/StreamController-class.html) for example
Comment: do you have example for using StreamBuilder than can listen if socketio class got something from socket.on?
Comment: i already sent you a link: https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html
Comment: It's hard to understand from that link
Comment: you have the video explaining that: `"Widget of the Week"` - also there is a lot of articles- tried `google("flutter streambuilder")`?
Comment: I alredy try it, it give me error. String can't be assigned to type stream.
`StreamBuilder builder = new StreamBuilder(
            stream: Provider.of(context).sender,
            builder: (context, snapshot) {
              return Text(snapshot.data?.toString() ?? 'Foo');
            },
          ),`
Comment: what is `Provider.of(context).sender`?
Comment: It's a Provider from provider library https://pub.dev/packages/provider.
In the bottom of the provider library page, there is an StreamProvider.

with that, I can keep update Chat object after got response from socket.on (so Chat.sender always keep changing after got new chat).

But I want to update the widget by rebuild not only the value
Comment: so use `ValueListenableProvider` or `StreamProvider` - its up to you how you do that - if you dont know how to use `Provider` library so why you use it anyway? maybe use `StreamBuilder` instead?
Comment: Because I don't know how to use StreamBuilder and I use Provider for another usage, but glad, I'm already got it. Although I don't know if this is the right way how to do it. I will update my question with my answer.
Comment: ok, so as i said: use `ValueListenableProvider` or `StreamProvider` - if i had to use that library  i would use `ValueListenableProvider` and `ValueNotifier`
Comment: Thanks for the recommedation, but I already got the headache so I will stick with StreamProvider for the solution that already working although it's feels weird.
Comment: what feels weird?
Comment: I solved it by hard coded.
as initial value, I give Chat.sender = "blank",
and I keep check it if not blank so must be another new message.
the code in the answer.

then inside model I have method detectNewChat that will update the widget
Answer: <code>        children: <Widget> [
          (Provider.of<Chat>(context).sender == "blank")
              ? Container()
              : model.detectNewChat(widget.roomId, Provider.of<Chat>(context)),
          Expanded(child: ListView(
            children: model.chats
                .map((chat) => ChatItem(chat, Provider.of<User>(context).id)).toList(),
          ),),
          new Divider(height: 1.0),
</code>
I'm solved it by using Provider.of(context).
here code in my socket.io
<code>  StreamController<Chat> chatController = StreamController<Chat>();

    socket.on('chat message', (data) => {

        print("got reply chat"),
        chatController.add(new Chat(data['roomId'], data['username'], "1123", data['message'], DateTime.now())),
    });
</code>
inside model
<code>  Widget detectNewChat(String currentRoomId, Chat newChat) {
    print(newChat.sender + " "+newChat.body);
    // TODO add filter if roomId not match with current room id
    if (currentRoomId == newChat.roomId) {
      addChat(newChat.roomId, newChat);
    }
    return Container();
  }

  addChat(String roomId, Chat chat) {
    setState(ViewState.Busy);

    _socketio.emitNewChat(roomId, chat.body);

    this.chats.add(chat);
    _isComposingMessage = false;
    _textEditingController.clear();

    setState(ViewState.Idle);
  }
</code>
So if there is a new reply, I can just call chatController.add(newChat).
because StreamProvider from Provider library will keep getting the last value.


(Android) How to disable scrolling of certain elements of nested recyclerview

Question: With this link you can navigate to the image of my recyclerview.
Here I have vertical recyclerview inside horizontal recyclerview. The horizontal scrolling works fine because I have used LinearSnapHelper. So everytime I scroll horizontally, one of the displays of date and hours is positioned in the center. The problem is that I want to disable the vertical scrolling of 2 recyclerviews of the image which are on the edges, so the only central recyclerview can be scrolled vertically at that moment.
Looking forward to suggestions!
Thanks in advance!
Comment: setNestedScrolling=false
Comment: @AslamHossin I've tried that, but it doesn't work, thanks anyway!
Answer: I would like to help you with your problem
Could you please submit code snippets of your main activity, main XML file, and the activity that this layout is put in?


Execute Dynamic query inside Select query in stored procedure SQL Server

Question: I am trying to execute dynamic generated select query as a column value of Select statement. I tried using functions but that don't allow to execute dynamic query block.
Can anyone have way around to get me the desired solution?
create table #temp(column1 nvarchar(10), column2 nvarchar(10), column3 nvarchar(10));
insert into #temp exec fetch_some_data;

select
columnX,
columnY,
(select COALESCE('column3', null) from #temp a where coalesce('a.column1', null) = 'somevalue') as columnZ
from TableName

P.S.: This columns of #temp are not fixed, they are created dynamically from reading csv. Just for explanation they are added
Comment: Please, post sample data and expected results.
Comment: "This columns of #temp are not fixed" <-- could you elaborate? The names of the columns can be different but also the number of columns in #temp can vary? eg. if you fetch Address.csv then #temp(Name, Address, City) and if you fetch Person.csv then #temp(Fname, Lname, Age, Email)?  Or is it that the number of columns of #temp is always the same (i.e 3) an only the column names differ for each csv? It would be very helpful If you could provide a more detailed example,  for two different csv files and the corresponding sql that needs to be executed.
Comment: @lptr "This columns of #temp are not fixed"- This means the csv will be having different number of columns and name won't be fix.
Comment: thanks for the info. Could you provide more details on the query? (select COALESCE('column3', null) from #temp a where coalesce('a.column1', null) = 'somevalue') as columnZ ? you want to use all columns of #temp or only the columns at position 1 and 3? Just write the query when #temp has col1, col2, col3, col4, col5. A bit more of explanation is needed.
Answer:
I tried using functions but that don't allow to execute dynamic query
  block.

You are on the right track. You can not use a normal tsql function (exec query limitation)  but you could use a scalar clr function.
How-to
Check the example of the clr function at https://learn.microsoft.com/en-us/sql/relational-databases/clr-integration-database-objects-user-defined-functions/clr-scalar-valued-functions?view=sql-server-ver15#example-of-a-clr-scalar-valued-function
for executing a dynamic statement, the sample code could be:
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;

public class T
{
    [SqlFunction(DataAccess = DataAccessKind.Read)]
    public static object ExecDynamicSql(string sql)
    {
        using (SqlConnection conn
            = new SqlConnection("context connection=true"))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            return cmd.ExecuteScalar();
        }
    }
}

compile it to a dll (either with visual studio or csc.exe)
then in ssms:
CREATE ASSEMBLY DynamicSqlAssembly
FROM 'C:\Users\..xyz...\Documents\Visual Studio xzy\Projects\xyz\xyz\bin\Debug\xyzname.dll'  --<-- path to dll here
WITH PERMISSION_SET = SAFE;
GO

CREATE FUNCTION dbo.FnExecSql(@query nvarchar(max))
RETURNS sql_variant
WITH RETURNS NULL ON NULL INPUT
AS EXTERNAL NAME DynamicSqlAssembly.T.ExecDynamicSql;
GO

select dbo.FnExecSql('select getdate()');

select name, dbo.FnExecSql('select count(*) from ' + name) as tablecount
from sys.tables;

For completeness, you might want to add exception handling to the clr code and possibly return null when an exception occurs.It depends on your requirements.


Showing $\rho_{Hom}\cdot \phi(v)=\rho_W\cdot \phi\cdot \rho_V^{-1} v$.

Question: I am wanting to show using the isomorphism $Hom(V,W) \cong W \otimes V^*$ that:
$$\rho_{Hom}\cdot \phi(v)=\rho_W\cdot \phi\cdot \rho_V^{-1} v$$
I know that we need to use the relation:
$$\rho_{W\otimes V^*}(w\otimes \lambda)=\rho_W w\otimes \rho_{V^*}\lambda$$
but am not sure how to correctly write $\rho_{Hom}\cdot \phi(v)$ in terms of $\rho_{W\otimes V^*}$ etc. Please can someone explain.
(Notation used: $\rho_V$ is the representation $\rho_V: G\rightarrow GL(V)$ for convenience $\rho_V\equiv \rho_V(g)$. $\phi \in Hom(V,W)$, $v \in V$, $w \in W$ and $\lambda \in V^*$.)
Comment: Can you explain all your notation?  What is $\phi$ and what are all the $\rho$s?
Comment: @EricWofsey see edit
Answer: The key point here is unraveling how the isomorphism $\operatorname{Hom}(V,W)\cong W\otimes V^*$ is defined.  Specifically, this isomorphism is the unique linear map that sends $w\otimes\lambda\in W\otimes V^*$ to the map $v\mapsto \lambda(v)w$ in $\operatorname{Hom}(V,W)$.  Morever, maps of the form $v\mapsto \lambda(v)w$ span $\operatorname{Hom}(V,W)$, and the equation you wish to prove is linear in $\phi$.  Thus in order to prove it, we may assume that $\phi$ has this form and corresponds to an element of the form $w\otimes \lambda\in W\otimes V^*$.
Now it's just a matter of computation.  Since $\rho_{W\otimes V^*}(w\otimes \lambda)=\rho_W w\otimes \rho_{V^*}\lambda$, this means that $\rho_{Hom}(\phi)$ is the map $V\to W$ which sends $v$ to $(\rho_{V^*}\lambda)(v)\rho_W w$.  We now have
\begin{align*}
(\rho_{Hom}\phi)(v)&=(\rho_{V^*}\lambda)(v)\rho_W w \\
&=\lambda(\rho_V^{-1}(v))\rho_W w \\
&=\rho_W(\lambda(\rho_V^{-1}(v))w) \\
&= \rho_W(\phi(\rho_V^{-1}(v)))
\end{align*}
just as you want. (The second equality is the definition of $\rho_{V^*}$, the third equality is linearity of $\rho_W$, and the last equality is just the fact that $\phi(x)=\lambda(x)w$ for any $x$.)
Comment: please could you expand on why $\rho_{W\otimes V^*}(w\otimes \lambda)=\rho_W w\otimes \rho_{V^*}\lambda$ means that $\rho_{Hom}(\phi)$ is the map $V\to W$ which sends $v$ to $(\rho_{V^*}\lambda)(v)\rho_W w$?
Comment: By definition of the isomorphism $\operatorname{Hom}(V,W)\cong W\otimes V^*$, the element $\rho_W w\otimes \rho_{V^*}\lambda\in W\otimes V^*$ corresponds to the map $v\mapsto (\rho_{V^*}\lambda)(v)\rho_W w$ in $\operatorname{Hom}(V,W)$.


Editing /etc/grub.d/40_custom

Question: Are there any available methods so that I can edit my /etc/grub.d/40_custom from a LiveCD or such? I really need to edit that file. Thank you.
Comment: To clarify: You have an Ubuntu installation on hdd that won't boot anymore. The reason is the mentioned grub config file. Your question is how to access the file system on the hdd from a LiveCD. Is that correct?
Comment: Nope, I specifically need to edit a line in that file as I have entered a wrong value there.
Comment: Isn't that pretty much what I asked? In order to edit the file, you need access to the filesystem. Obviously your OS is not booting anymore, because otherwise you could edit the mentioned file the same way you did before. If Grub fails, the way to go is booting from a LiveCD. I won't type out an answer, since there are myriads of tutorials on the subject around, e.g. http://howtoubuntu.org/how-to-repair-restore-reinstall-grub-2-with-a-ubuntu-live-cd
In your case you probably won't need to reinstall Grub, but only update-grub after editing the file. Good luck! Ask back if you need furher help.
Comment: I have already tried it out, also there aren't any mention of editing the mentioned file anywhere in Google. Thank you.
Comment: *"I specifically need to edit a line in that file as I have entered a wrong value there"* > If your only issue is booting on this entry, then simply edit it from the grub boot menu with the 'e' key.
Answer:
Boot a Live System from CD/DVD/USB
Assuming the root partition of the Ubuntu installation on the hdd is sda1, mount it to /mnt
sudo mount /dev/sda1 /mnt
If the root filesystem is not on on sda1 use e.g sudo fdisk -l to find the correct identifier.
Open the file /mnt/etc/grub.d/40_custom with your favorite editor, e.g
sudo nano /mnt/etc/grub.d/40_custom

Make any changes you feel necessary. Best to make a backup beforehand.
Now you need to update Grub.
a. Mount the kernel filesystems to their appropriate places
    sudo mount --rbind /dev /mnt/dev
    sudo mount --rbind /sys /mnt/sys
    sudo mount -t proc proc /mnt/proc

b. Do you have a separate boot partition?
  If yes, mount it to /mnt/boot
c. Change root to /mnt
    sudo chroot /mnt

d. Issue update-grub. This will generate a new grub.cfg and write it to /boot/grub/. (Btw, this is equivalent to grub-mkconfig -o /boot/grub/grub.cfg)
Reboot.
Comment: My old 40_custom is not visible. A new 40_custom is created with read-only permission. Thank you for your efforts.
Comment: I followed this - http://howtoubuntu.org/how-to-repair-restore-reinstall-grub-2-with-a-ubuntu-live-cd along with the workaround.
Comment: @Raphael I'm not really happy with the edit you made. The fact that you couldn't 'edit the file' has imho nothing to do with `nano`. What was the error? Did the file even exist? If not, was the very first `mount` command successful? I ask because you apparently went on to edit `/etc/grub.d/40_custom` which is weird, because __before__ the `chroot` this file belongs to the _Live_ filesystem, not the one you mounted (or maybe didn't?) to `/mnt` !
Comment: I edited it after chroot, and also nano couldn't detect the file whereas the file existed when I browsed it through dolphin i.e., after I have mounted sda and changed root. I edited it cause it might have helped somebody else. Oh well!

Editing /etc/grub.d/40_custom


Java PrintWriter output looks weird in notepad

Question: When I look at the file using mousepad on Linux, it looks correct. When I look at the file on Windows XP or Windows 7 with notepad, the output is all on the same line and is incorrect.
Here is the (correct) output for out.txt I'm getting on Linux:
  3.40        4.50        5.60
  3.40        4.50        5.60
  3.40        4.50        5.60

EDIT: I encoded the text with CR+LF and it looks right, but how do I get java to export in that fashion?
Here's the code, it's just for testing this.
import java.io.*;
import java.util.*;
public class untitled {

    public static void main (String args[]) throws FileNotFoundException {

        PrintWriter output = new PrintWriter("out.txt");

        double number1 = 3.4;
        double number2 = 4.5;
        double number3 = 5.6;

        for (int x=0; x<3; x++) {
        output.printf("%6.2f",number1);
        output.printf("%12.2f",number2);
        output.printf("%12.2f",number3);
        output.println("");
        }

        output.close();
    }
}

Thanks!
Comment: See this http://en.wikipedia.org/wiki/Newline. The newline standards are different between Linux and Windows.
Comment: Notepad is the worst tool to see text file and sometimes ignores tabs/newlines. Try opening the file in a another text editor such as Editplus(commercial) or Notepad++/Crimson Editor. It should display just fine.
Comment: run your code in windows and see the output
Comment: Hi all, I have to use notepad for school :(. Mystical, I did LF+CR and it looks right in Windows, how do I tell Java to export in that format though? That's the important thing.
Comment: I'm not sure if there's a "standard" way to do it in Java. (hence why I didn't post it an answer) But I suppose `output.print("\r\n")` will work. I can make this an answer if you want.
Comment: That makes sense, I guess my final question is. If compiled in windows, it will look right in notepad right? THanks so much for your time.
Comment: If you *run* it in Windows it should be correct. (I don't think it matters where you compile it though.) Someone correct me if I'm wrong.
Comment: *Everything* looks weird in Notepad :-|
Answer: The reason it looks "weird" is because the Windows convention/standard for separating lines is Carriage Return, Line Feed ("\r\n"), as opposed to the *nix way of just a line feed ("\n"). Therefore if you use a basic editor such as notepad, everything will seem to be on the same line. Better editors such as Notepad++, or even wordpad, will display the text file properly even if it's in *nix format.
If you run your Java program in Windows, the line separators will be set appropriately, however you can also change the line separator explicitly so that it is always "\r\n" by using System.setProperty() with the "line.separator" key.
Comment: I should note that I compile it in linux and took the output file that was made in linux, emailed it to myself and looked at it on windows... if I were to simply compile it on windows, would it look right? Thanks for the comment, that makes somewhat sense to me, this is a new concept
Comment: @user1062058: On Linux the `"line.separator"` key will be set to `"\n"` and on Windows it'll be `"\r\n"`. So yeah, if you run your program on Windows, the output will look *"right"*.
Answer: If you do printf("%n"); Java will print the appropriate line separator for your platform.


The 'Learn more' or 'Book now' Call to Action problem on overview pages

Question: I need help thinking this through: There are holiday packages to be sold so I have an overview template that shows the collection of packages and a single detail template that explains the details (of course).
Now on the overview page the client insists on having both a learn more and book now button. I think the book now button is redundant there because no one is going to book a package without having seen the details.
So now my questions are:

How do you weigh the choices and rule out the wrong one?
How do I measure which one was right?
Does it make sense to say that one choice is primary and the other is secondary?
Answer: It all comes down to two factors: business strategy and user needs.
It seems like you have a pretty clear pressure from the business strategy side of the world to include both CTAs; what do you have from the user side of things? Do you have any research to support one view or the other? Have you tried using usertesting.com with a quick prototype or perhaps one of the OptimalSort tools?
As for measuring which is right, if you are in an iterative environment (as in, you can change it if it performs horribly) do it the way they say, establish clear measures of success (low abandon rates, high conversion, etc.) and see what happens.
It's best practice to have one be primary and the other be secondary. My initial and ignorant view is that "learn more" would be primary and "book now" would be secondary. If someone clicks "learn more" they can still "book now" on the next page. If someone clicks "book now" and was really wanting to "learn more" they need to abandon their current path.


Grand Central Dispatches - execution out of order?

Question: I'm trying to get a better understanding of GCD so I wrote the test code below (Bottom).  Essentially two functions with waits inside sent on different queues and println on main thread waiting for particular tasks.
I expect the console output should be:
BEFORE FUNCTIONS
start 3 sec loop
start 5 sec loop
end 3 sec loop
BETWEEN FUNCTIONS WAIT ON LONG
AFTER FUNCTIONS WAIT ON LONG
end 5 sec loop
BETWEEN FUNCTIONS WAIT ON LONGER
AFTER FUNCTIONS WAIT ON LONGER

but what I get instead is:
BEFORE FUNCTIONS
start 3 sec loop
start 5 sec loop
BETWEEN FUNCTIONS WAIT ON LONGER
AFTER FUNCTIONS WAIT ON LONGER
end 3 sec loop
BETWEEN FUNCTIONS WAIT ON LONG
AFTER FUNCTIONS WAIT ON LONG
end 5 sec loop

a number of things don't make sense to me - 1.  Why are the "Longer" ones printed before the "Long"?  2.  Why are the "Longer" ones not printed after the END of the longer i.e. 5 second function?
CODE (ViewController.swift)
import UIKit

class ViewController: UIViewController {

  var longQueue = dispatch_group_create()
  var longerQueue = dispatch_group_create()
  var queueLow = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)
  var queueHigh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    println("BEFORE FUNCTIONS")

    dispatch_async(queueHigh) {
      self.longFunc()
    }

    dispatch_group_notify(longQueue, dispatch_get_main_queue()) {
      println("BETWEEN FUNCTIONS WAIT ON LONG")
    }
    dispatch_group_notify(longerQueue, dispatch_get_main_queue()) {
      println("BETWEEN FUNCTIONS WAIT ON LONGER")
    }

    dispatch_async(queueLow) {
      self.longerFunc()
    }

    dispatch_group_notify(longQueue, dispatch_get_main_queue()) {
      println("AFTER FUNCTIONS WAIT ON LONG")
    }
    dispatch_group_notify(longerQueue, dispatch_get_main_queue()) {
      println("AFTER FUNCTIONS WAIT ON LONGER")
    }
  }

  func longFunc () {
    dispatch_group_enter(self.longQueue)
      println("start 3 sec loop")
      sleep(3)
      println("end 3 sec loop")
    dispatch_group_leave(self.longQueue)
  }

  func longerFunc() {
    dispatch_group_enter(self.longerQueue)
      println("start 5 sec loop")
      sleep(5)
      println("end 5 sec loop")
    dispatch_group_leave(self.longerQueue)
  }

}

Answer: There are two categories of problems here:

You are entering the dispatch groups inside the longFunc and longerFunc. But, because you're dispatching the calls to those functions to their respective queues, you have no assurances that you're going to reach dispatch_group_enter before you reach the dispatch_group_notify calls you perform in viewDidLoad on the main thread. Also remember that the code that is dispatched to the background queues runs asynchronously and we have no assurances when it runs versus the code that is continuing to execute on the main thread. The key is that if it encounters dispatch_group_notify before you've even entered the group, that notify closure will fire immediately.
To avoid this race condition, you either want to perform the dispatch_group_enter in viewDidLoad before you dispatch the code to the background queues (and obviously, before you set up the notify closure), or you want to just retire the entering and leaving of the groups and then replace dispatch_async with  dispatch_group_async. Either of those approaches ensure that you enter groups before you add the notify closures to those groups.

The dispatch notify closure that reports "BETWEEN FUNCTIONS WAIT ON LONGER" is an even more egregious example, where there's not even a race condition. You are simply adding that notify closure before you even dispatch the function that will later enter the group. So this will almost certainly fire before you code is dispatched to that queue.
Comment: Thank you for your reply.  I did not know the dispatch_group_notify(queueX) block would just execute if queueX is not setup yet.  Thinking about it, I guess a non-existant queueX is potentially similar to an empty queueX (not sure how GCD deals with finished queues).  I've tried both your suggestions - placing the dispatch_group_enter before the dispatch_async as well as switching to dispatch_group_async (cleaner).  Both result in expected output (BETWEEN WAIT ON LONGER still fires first but understandably so).
Answer: When you queue a block using dispatch_async, there may be some time before that block begins running, during which the queueing thread (the main thread, in your case) continues to run. Then again, the queued block may begin running right away, before the next statement in the queueing thread runs.
When you call dispatch_group_notify, if the group's entry count is zero at that moment, dispatch_group_notify will immediately queue the notification block for execution. But queuing a block for execution is different than running the block! If the dispatch queue is busy, it won't run the notification block right away.
Let's look at how (I deduce) things happened to produce your actual output. After each statement that had some important effect, I'll describe what it did. I'll also put the print actions in bold.
  override func viewDidLoad() {
    super.viewDidLoad()

    println("BEFORE FUNCTIONS")

That line printed BEFORE FUNCTIONS to standard output.
    dispatch_async(queueHigh) {
      self.longFunc()
    }

You queued longFunc to run on queueHigh. It began running immediately and called dispatch_group_enter on longQueue, raising longQueue's entry count to 1. It also printed start 3 sec loop. Then it went to sleep for 3 seconds. It's on a background thread, so this didn't block the main thread.
    dispatch_group_notify(longQueue, dispatch_get_main_queue()) {
      println("BETWEEN FUNCTIONS WAIT ON LONG")
    }

You submitted a notification block to print BETWEEN FUNCTIONS WAIT ON LONG when longQueue's entry count is zero. It's not zero right now, because longFunc entered but hasn't left yet, so the notification block is added to the group, not to the main queue.
    dispatch_group_notify(longerQueue, dispatch_get_main_queue()) {
      println("BETWEEN FUNCTIONS WAIT ON LONGER")
    }

You submitted a notification block to print BETWEEN FUNCTIONS WAIT ON LONGER when longerQueue's entry count is zero. It's zero now, because longerFunc hasn't even been queued yet, so the notification block is immediately added to the main queue. But the main thread is busy running this method (viewDidLoad) right now, so it can't drain the main queue yet.
    dispatch_async(queueLow) {
      self.longerFunc()
    }

You submitted longerFunc to queueLow. It doesn't start running right away.
    dispatch_group_notify(longQueue, dispatch_get_main_queue()) {
      println("AFTER FUNCTIONS WAIT ON LONG")
    }

You submitted a notification block to print AFTER FUNCTIONS WAIT ON LONG when longQueue's entry count is zero. It's not zero right now, because longFunc entered but hasn't left yet, so the notification block is added to the group, not the main queue.
    dispatch_group_notify(longerQueue, dispatch_get_main_queue()) {
      println("AFTER FUNCTIONS WAIT ON LONGER")
    }

You submitted a notification block to print AFTER FUNCTIONS WAIT ON LONGER when longerQueue's entry count is zero. It's zero now, because longerFunc hasn't started yet, so the notification block is immediately added to the main queue. But the main thread is busy running this method (viewDidLoad) right now, so it can't drain the main queue yet.
  }

You returned from viewDidLoad, which lets main thread continue execution of the main run loop. Here's what happened next:

longerFunc started running. It called dispatch_group_enter on longerQueue, raising longerQueue's entry count to 1. Note that this does not retroactively remove those notification blocks from the main queue! They were queue at a time when longerQueue's entry count was zero, and they will remain on the main queue until they run, regardless of what happens to longerQueue's entry count later.
longerFunc printed start 5 sec loop and then went to sleep for 5 seconds. This is on its own background thread, so it doesn't block the main thread or the thread running longFunc.
The main thread's run loop dequeued a block from the main queue and executed it. This was the first notification block you submitted for longerQueue. It printed BETWEEN FUNCTIONS WAIT ON LONGER.
The main thread's run loop dequeued another block from the main queue and executed it. This was the second notification block you submitted for longerQueue. It printed AFTER FUNCTIONS WAIT ON LONGER.
The main thread's run loop didn't find any more blocks waiting on the main queue. It went to sleep.
The longFunc function finished its 3 second sleep. It printed end 3 sec loop.
The longFunc function called dispatch_group_leave on longQueue. This lowered the entry count of longQueue to zero, so the group submitted all pending notification blocks to the main queue. This act woke up the main thread's run loop.
The main thread's run loop dequeued another block from the main queue and executed it. This was the first notification block you submitted for longQueue. It printed BETWEEN FUNCTIONS WAIT ON LONG.
The main thread's run loop dequeued another block from the main queue and executed it. This was the second notification block you submitted for longQueue. It printed AFTER FUNCTIONS WAIT ON LONG.
The main thread's run loop didn't find any more blocks waiting on the main queue. It went to sleep.
The longerFunc function finished its 5 second sleep. It printed end 5 sec loop.
The longerFunc function called dispatch_group_leave on longerQueue. This lowered the entry count of longerQueue to zero. The longerQueue group had no pending notification blocks, so it did nothing.

The end.
Comment: Thank you for your detailed run through it helps to explain things.  One question though:  After the "}" in viewDidLoad you mention longFunc and longerFunc running on the main queue.  Aren't those running on queueHigh and queueLow respectively?
Comment: I don't think I said they run on the main queue. I said the main thread continues execution of the main run loop. The function run on their own threads, which I mention when I say they went to sleep for 3/5 seconds.


Exposing python daemon as a service

Question: So, there are two very valuable features that I am already able to extract from a python script. The first is the ability to run a python function as a service from the command line. Assuming that the python script takes in command line args for simplicity. Something along the lines of:
import sys

def foo():
    return "%s is your last argument!" % sys.argv[-1]

foo()

which I would then access globally by running python file.py someargand additionally, I could write up a supervisord script to daemonize a script and keep it running in memory. I now find myself in a position where I need both of these features at once and I'm not really sure where to start on this. For clarity, I basically have something along these lines:
if __name__ == "__main__":
    big_file = open(slow_loader)
    foo(big_file)

Where ideally, once this is running I would be keeping the entire big_file in memory and be able to access the foo method depending on that big_file by running something akin to the original python file.py somearg. I'm not really sure how to progress from here though.
Any help, even if it's just a link to some documentation would be very helpful. Ahead of time, I realized I could wrap this in a shallow flask app and run it through http requests, but for NDA'd reasons I need something that runs through an internal shell command.
Comment: Is introducing something like redis or memcached a possibility?
Comment: @dano I can totally add any external service, but not to replace the big_file. Technically I can't actually touch the source for for the `file.py`, but I'm confident enough in introspection that once the main solution is there I can apply it to this context.
Answer: Just because I like zmq and gevent, I would probably do something like this:
server.py
import gevent
import gevent.monkey
gevent.monkey.patch_all()
import zmq.green as zmq
import json

context = zmq.Context()
socket = context.socket(zmq.ROUTER)
socket.bind("ipc:///tmp/myapp.ipc")

def do_something(parsed):
    return sum(parsed.get("values"))

def handle(msg):
    data = msg[1]
    parsed = json.loads(data)
    total = do_something(parsed)
    msg[1] = json.dumps({"response": total})
    socket.send_multipart(msg)

def handle_zmq():
    while True:
        msg = socket.recv_multipart()
        gevent.spawn(handle, msg)

if __name__ == "__main__":
    handle_zmq()

And then you would have a client.py for your command line tool, like
import json
import zmq

request_data = {
        "values": [10, 20, 30 , 40],
        }

context = zmq.Context()
socket = context.socket(zmq.DEALER)
socket.connect("ipc:///tmp/myapp.ipc")
socket.send(json.dumps(request_data))
print socket.recv()

Obviously this is a contrived example, but you should get the idea.  Alternatively you could use something like xmlrpc or jsonrpc for this as well.
Comment: `gevent` is a `greenlet`-based asynchronous networking framework. That call is running `handle` in a greenlet, so that it can asynchronously handle `msg` while still listening for new requests. Note that it only makes the I/O asynchronous, though. It may or may not be useful to you, depending on what kind of work you're doing in your implementation of `handle` . If you're doing something that's CPU-bound, you'd probably want to use `multiprocessing` instead. And being asynchronous may not mean much at all if you're not expecting concurrent requests to come into `server.py`.
Comment: Yeah, I was going to suggest using something-AMQP based, but ZeroMQ would work fine, too.
Comment: Sorry, any chance you could explain a little more explanation around the `gevent.spawn` and `gevent.monkey.patch_all` bits?

Exposing python daemon as a service


Replace letters with integer and place "-" (dash) with it

Question: I am currently using the following JavaScript code:
concatedSubstring.replace(/\//g, '-').replace(/[A-Za-z]/g, function(c){
    return c.toUpperCase().charCodeAt(0)-64;
});

...to take input in the format "1234/A", "22/B", etc. and output "1234-1" , "22-2", etc.
That is, / becomes -, and the letters become integers with A = 1, B = 2, etc.
I would like to change this so that if the input doesn't contain a "/" the output will still insert a "-" in the spot where the "/" should've been. That is, the input "1234A" should output "1234-1", or "22B" should output "22-2", etc.
Comment: So "1234B" should turn into "1234-2" (your question has "1234-1")?
Comment: sorry my mistake 1234B must be replaced with 1234-2
Comment: Do you know what the word *alphabet* means in English?  Not the way you're using it.  There are things like the Latin alphabet, the Greek alphabet, the Phoenician alphabet, etc.
Comment: Tho grammatically you are correct I should be using word letter but at that moment the 1st thing that popped to my mind was alphabet, As English isn't my native language. But I think my question explains what I am trying to do.
Answer:
str = "1234/B"; or str = "1234B";
str.replace(/(\/[A-Z])|([A-Z])/g,"-"+parseInt(str.charCodeAt(str.indexOf(str.match(/[A-Z]/g)))-64))
Comment: Doesn't this do what the code in the question already does? OP wants to insert a hyphen even when there isn't a forward slash, on input like "1234B".
Comment: str = "1234B";

str.replace(/(\/[A-z])|([A-z])/g,"-"+parseInt(str.charCodeAt(str.indexOf(str.match(/[A-z]/gi)))-64))
Comment: Note also the range `A-z` includes some non-alphabet characters.
Answer: The following should work even for inputs containing more than one of your number/letter pattern:
var input = "1234/B 123a 535d";

var replaced = input.replace(/(\d+)(\/?)([A-Za-z])/g, function(m,p1,p2,p3) {
   return p1 + "-" + (p3.toUpperCase().charCodeAt(0)-64);
});

alert(replaced); // "1234-2 123-1 535-4"

The regex:
/(\d+)(\/?)([A-Za-z])/g

...will match one or more digits followed by an optional forward slash followed by a single letter, capturing each of those parts for later use.
If you pass a callback to .replace() then it will be called with arguments for the full match (which I'm ignoring for your requirement) and also for any sub-matches (which I use).
Answer: You can also .replace(/([0-9])([a-zA-Z])/g,"$1-$2"): this turns a number adjacent to a letter into numberDASHletter, using backreferences (the $1 refers to whatever was in the first set of brackets, $2 to whatever was in the second set of brackets).


How to show the list of shortcuts produced by super key using command line

Question: What is the command that shows the same result of holding the super key?!
I.e. What is the command that list all shortcuts in system...
Comment: Why would there be one? That's an internal Unity feature, I doubt there is any CLI command behind it.
Answer: You can get some of them using:
<code>$ gsettings list-recursively org.gnome.desktop.wm.keybindings | grep -v '\[\]' | cut -f 1 -d ' ' --complement
cycle-windows-backward ['<Shift><Alt>Escape']
minimize ['<Primary><Alt>KP_0']
move-to-monitor-left ['<Super><Shift>Left']
unmaximize ['<Primary><Super>Down', '<Super>Down', '<Alt>F5']
panel-main-menu ['<Super>s', '<Alt>F1']
cycle-windows ['<Alt>Escape']
cycle-panels-backward ['<Shift><Control><Alt>Escape']
panel-run-dialog ['<Alt>F2']
switch-to-workspace-down ['<Control><Alt>Down']
move-to-corner-ne ['<Primary><Alt>KP_Prior']
move-to-side-e ['<Primary><Alt>KP_Right']
move-to-workspace-right ['<Control><Shift><Alt>Right']
move-to-workspace-up ['<Shift><Control><Alt>Up']
maximize ['<Primary><Super>Up', '<Super>Up', '<Primary><Alt>KP_5']
cycle-group-backward ['<Shift><Alt>F6']
move-to-side-n ['<Primary><Alt>KP_Up']
move-to-side-s ['<Primary><Alt>KP_Down']
move-to-side-w ['<Primary><Alt>KP_Left']
move-to-corner-nw ['<Primary><Alt>KP_Home']
begin-move ['<Alt>F7']
move-to-monitor-down ['<Super><Shift>Down']
move-to-workspace-left ['<Control><Shift><Alt>Left']
move-to-corner-se ['<Primary><Alt>KP_Next']
activate-window-menu ['<Alt>space']
switch-input-source ['<Super>space']
move-to-monitor-right ['<Super><Shift>Right']
switch-input-source-backward ['<Shift><Super>space']
move-to-workspace-last ['<Super><Shift>End']
cycle-panels ['<Control><Alt>Escape']
move-to-monitor-up ['<Super><Shift>Up']
move-to-corner-sw ['<Primary><Alt>KP_End']
toggle-shaded ['<Primary><Alt>s']
switch-to-workspace-1 ['<Primary><Mod2>Alt_L']
switch-panels ['<Control><Alt>Tab']
switch-panels-backward ['<Shift><Control><Alt>Tab']
switch-applications-backward ['<Shift><Super>Tab', '<Shift><Alt>Tab']
show-desktop ['<Primary><Super>d', '<Primary><Alt>d', '<Super>d']
switch-applications ['<Super>Tab', '<Alt>Tab']
switch-to-workspace-left ['<Control><Alt>Left']
toggle-maximized ['<Primary><Alt>KP_5']
begin-resize ['<Alt>F8']
move-to-workspace-down ['<Shift><Control><Alt>Down']
switch-to-workspace-last ['<Super>End']
switch-group-backward ['<Shift><Super>Above_Tab', '<Shift><Alt>Above_Tab']
switch-group ['<Super>Above_Tab', '<Alt>Above_Tab']
cycle-group ['<Alt>F6']
close ['<Alt>F4']
switch-to-workspace-up ['<Control><Alt>Up']
move-to-workspace-1 ['<Super><Shift>Home']
switch-to-workspace-right ['<Control><Alt>Right']
</code>
and:
<code>$ dconf read /org/compiz/integrated/show-hud
['<Alt>l']
</code>
Unity Lens shortcuts (Super+) are defined here:
<code>$ grep Shortcut /usr/share/unity/scopes/*.scope
/usr/share/unity/scopes/applications.scope:Shortcut=a
/usr/share/unity/scopes/files.scope:Shortcut=f
/usr/share/unity/scopes/music.scope:Shortcut=m
/usr/share/unity/scopes/photos.scope:Shortcut=c
/usr/share/unity/scopes/video.scope:Shortcut=v
</code>
Unity shortcuts such as Super+<1 to 9> are an internal to Unity an cannot be listed from the command line.
Comment: +1 Even it's not complete set. I'll consider it as an answer if I don't find a better solution
Comment: @NewUSer: Thanks, I think that except HUD/unity harcoded internals, the first command should give you a pretty good list.


Whats the trick in javascript Array.findIndex

Question: I was trying to simulate random by probabilities, and found something interesting with Array.findIndex
const probabilities = [0.1, 0.2, 0.3];
const thresholds = probabilities.reduce((r, p, i) => r.concat((r[i - 1] || 0) + p), []);

const f1 = () => thresholds.findIndex(x => x > Math.random());
const f2 = () => {
    const r = Math.random();
    for (let i = 0; i < thresholds.length; i++)
        if (r < thresholds[i])
            return i;
    return -1;
};

const run = (f) => Array.from(Array(1000000), f).reduce((r, x) => Object.assign(r, { [x]: 1 + (r[x] || 0) }), {});

run(f1);
run(f2);

The output results were like
{ "0": 100210, "1": 269763, "2": 377463, "-1": 252564 }
{ "0": 99381,  "1": 199927, "2": 300408, "-1": 400284 }

Apparently the Array.findIndex paired with Math.random was not working as I supposed.
Comment: What's your question?
Comment: Hi, Welcome to SO, Please go read [How to Ask](https://stackoverflow.com/help/how-to-ask). Code relevant to your problem belongs directly to your question, in text form and properly formatted.
Answer: You are comparing against a new, different random number every time, so the results are unpredictable. In your second function you don't do that, you create one number and save it to r and then check all thresholds with it.
You can do the same with findIndex as well:
const f1 = () => {
  const r = Math.random();
  return thresholds.findIndex(x => x > r);
};


WAMP : start mysql through PHP and Batch file

Question: How to start mysql server through PHP code by calling the Batch file in exec() or popen() ?
I'm using wampserver 3 64bit.
Comment: Try `exec("NET START wampmysqld");`
Comment: Done :). Glad it worked for you.
Comment: It is working fine. My mysqld name is wampmysqld64. Please post as answer. I'll mark as accepted.
Comment: You can answer your own question. @doe
Comment: @paskl Yes but Matt will get some reputation. :-)
Answer: For WAMPSERVER
To start:
exec("NET START wampmysqld64");

To stop:
exec("NET STOP wampmysqld64");

Comment: I think if he is using WAMPServer 64bit the 2 services are actually called `wampmysqld64` and `wampapache64`
Comment: @RiggsFolly Yep, I'll change that to suit :)


How to hide/show td based on drop down menus

Question: I have created a table having 5 or 6 td. The data in table is generated by 3 drop down menus with a Go button. When I set values in all the three drop downs, it shows me result in table.
But the problem is If i don't select a value in one or two of drop downs and set it to null, it should not show me that column in the table. I am trying it with javascript but i don't know how to do.
Here is my code for HTML:

$(document).ready(function() {
  $("#go").click(function() {
    var select1 = document.getElementById("select1").value;
    var select2 = document.getElementById("select2").value;
    var select3 = document.getElementById("select3").value;;
  });

  if (select1 == null) {
    document.getElementByClass('select1td').style.display = none;
  }
  if (select2 == null) {
    document.getElementByClass('select2td').style.display = none;
  }
});
<select id="select1" name="select1" style="width: 190px; display: block;">
  <option selected value="" disabled="disabled">Select an option</option>
  <?php
    $sql="SELECT DISTINCT name FROM tbl1 ";

    $result = mysql_query($sql);

    while ($row = mysql_fetch_array($result)) {

        echo "<option  class='name' value=' " . $row['name'] ."'>" . $row['name'] ."</option>";
        }
    ?>
</select>
Lot Name
<select id="select2" name="select2" style="width: 190px; display: block;">
  <option selected value="" disabled="disabled">Select an option</option>
  <?php
    $sql="SELECT DISTINCT course FROM tbl1 ";

    $result = mysql_query($sql);

    while ($row = mysql_fetch_array($result)) {

        echo "<option  class='course' value=' " . $row['course'] ."'>" . $row['course'] ."</option>";
        }
    ?>
</select>

      <?php echo $row["name"]; ?>

      <?php echo $row["course"]; ?>

      <?php echo $row["reg"]; ?>

Comment: please consider using `MySQLi` or `PDO` extensions. The **OLD** `MySQL` extension is deprecated and not recommended to be used.
Comment: I have to show If(select==null){then display that td i.e. column to none}
Answer: I changed your original javascript code to jquery. You can check empty values with if(myvalue). This works because javascript will see the value of an empty string as false. You can also do if(myvalue !== '') this checks if myvalue is not an empty string.
Also you wrote your if statements outside of the onclick event handler. Therefore the code was executed on the ready event.

$(document).ready(function() {
  $("#go").on('click', function() {
    $('#select1').val() ? $('.select1td').show() : $('.select1td').hide();
    $('#select2').val() ? $('.select2td').show() : $('.select2td').hide();
    $('#select3').val() ? $('.select3td').show() : $('.select3td').hide();

    /* This is an alternative notation if you prefer this.

    if ($('#select1').val()) {
      $('.select1td').show();
    } else {
      $('.select1td').hide();
    }

    if ($('#select2').val()) {
      $('.select2td').show();
    } else {
      $('.select2td').hide();
    }

    if ( $('#select3').val()) {
      $('.select3td').show();
    } else {
      $('.select3td').hide();
    }

    */
  });
});

<select id="select1">
  <option value=''>no value</option>
  <option value='something'>value</option>
</select>

<select id="select2">
  <option value=''>no value</option>
  <option value='something'>value</option>
</select>

<select id="select3">
  <option value=''>no value</option>
  <option value='something'>value</option>
</select>

<button id="go">Go</button>

    select1td
    select2td
    select3td

Comment: Can u please give me an example of how to do all this with data in table coming from database?
Comment: @stcara It's a bit unclear what you mean? Can you edit your question and describe in detail what you want to achieve?
Answer: In JQuery you can use element.toggle();
But is ok with javascript(element.style.display).
I think i see your problem, you write:
document.getElementByClass('select1td').style.display=none; //wrong

to get an element that has a specific class you must do like that:
document.getElementsByClassName('select1td')[index].style.display=none;

When you select a class you are selecting more than one element, you select an array of elements even you have only 1 element in array.
That is why you must specify a index.
Remember that in arrays index starts from 0(for taking the first element you must select element with index 0 like that):
document.getElementsByClassName('select1td')[0].style.display=none;
Improvement!
You can have only one class for those 3 td elements.

<?php echo $row["name"]; ?>
<?php echo $row["course"]; ?>
<?php echo $row["reg"]; ?>

And you select each one like that:
document.getElementsByClassName('selecttd')[0.style.display=none;//for the first one
document.getElementsByClassName('selecttd')[0].style.display=none;//second
document.getElementsByClassName('selecttd')[0].style.display=none;//third

Another Improvement!
You can use document.querySelector() //with this you can select all kind of elements for example by tag name,class,id,:
document.querySelector("td");//select the first td
document.querySelectorAll(".class")[2];//selects the third element from class
document.querySelector("#id");//selects an element by id

Your javascript code ca be:
$(document).ready(function(){
      $("#go").click(function(){
 var select1=document.getElementById("select1").value;
  var select2=document.getElementById("select2").value;
   var select3=document.getElementById("select3").value;
   ;
      });

      if(select1==null){
document.querySelectorAll('selecttd')[0].style.display=none;
      }
      if(select2==null){
document.querySelectorAll('selecttd')[1].style.display=none;
      }
      });
  </script>

And HTML:

<?php echo $row["name"]; ?>
<?php echo $row["course"]; ?>
<?php echo $row["reg"]; ?>

JQuery example:
$(document).ready(function(){
      $("#go").click(function(){
 var select1=$(#select1").val;
  var select2=$(#select2").val;
   var select3=$(#select3").val;
   ;
      });

      if(select1==null){
$('.selecttd')[0].toggle();
      }
      if(select2==null){
$('.selecttd')[1].toggle();
      }
      });

I hope this was helpful!
Good luck!
Answer: You can try this example: https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp
By the way, it is not recommended to use mysql* functions anymore. They are deprecated. Use mysqli* instead.


Adding a CheckBox to WPF MessageBox

Question: The Message Boxes of WPF could be customized as i understand.
I was wondering is it possible to add a CheckBox to the WPF MessageBox with say - Don't show this message again etc.?
Answer: Possible, you can change the WPF control styles and templates as per your requirement, see these links for further references:
Custom Message Box
http://blogsprajeesh.blogspot.com/2009/12/wpf-messagebox-custom-control.html
http://www.codeproject.com/Articles/201894/A-Customizable-WPF-MessageBox
http://www.codeproject.com/Articles/22511/WPF-Common-TaskDialog-for-Vista-and-XP
Answer: Could just use a Window
Passed checked in the ctor so you can get the value back
bool checked = false;
Window1 win1 = new Window1(ref input);
Nullable<bool> dialogResult = win1.ShowDialog();
System.Diagnostics.Debug.WriteLine(dialogResult.ToString());
System.Diagnostics.Debug.WriteLine(checked.ToString());

Answer: I realize this is a very old thread, but I was searching this matter today and was surprised to see no replies mentioning Ookii: https://github.com/ookii-dialogs/ookii-dialogs-wpf
I was already using it for Folder Browsing. Now I wanted to add a "Don't Show Again" checkbox whenever the main window is closed, and it's really simple to use it.
Here's my code:
using Ookii.Dialogs.Wpf;

//create instance of ookii dialog
TaskDialog dialog = new();

//create instance of buttons
TaskDialogButton butYes = new TaskDialogButton("Yes");
TaskDialogButton butNo = new TaskDialogButton("No");
TaskDialogButton butCancel = new TaskDialogButton("Cancel");

//checkbox
dialog.VerificationText = "Dont Show Again"; //<--- this is what you want.

//customize the window
dialog.WindowTitle = "Confirm Action";
dialog.Content = "You sure you want to close?";
dialog.MainIcon = TaskDialogIcon.Warning;

//add buttons to the window
dialog.Buttons.Add(butYes);
dialog.Buttons.Add(butNo);
dialog.Buttons.Add(butCancel);

//show window
TaskDialogButton result = dialog.ShowDialog(this);

//get checkbox result
if (dialog.IsVerificationChecked)
{
    //do stuff
}

//get window result
if (result != butYes)
{
    //if user didn't click "Yes", then cancel the closing.
    e.Cancel = true;
    return;
}

Adding a CheckBox to WPF MessageBox


How to change iPython error highlighting color

Question: I'm using IPython with iterm2 in macOS. I had never had issues before with the color scheme, but this time when an exception occurs, it highlights certain parts in a color combination that I find very hard to read. I've tried with different color setups in iterm and also adjusting highlighting_style and colors in the ipython_config.py file, without much luck. I've seen there is an option to set specific colors highlighting_style_overrides but I haven't been lucky finding the right pygments option for this.
See Position below. This is the best contrast setup I've achieved, I still find hard it to read without focusing.
Comment: Hi, I am facing the same issue. Your problem resolved yet?
Comment: No, I haven't been able to find a solution yet
Comment: Workaround: https://github.com/ipython/ipython/pull/13756#issue-1370412994
Answer: NOTE: Use Evan Grim's answer below for a persistent config without touching package files.

There is an open issue regarding this: https://github.com/ipython/ipython/issues/13446
Here is the commit which introduced this change:
https://github.com/ipython/ipython/commit/3026c205487897f6874b2ff580fe0be33e36e033
To get the file path on your system, run the following:
import IPython, os
os.path.join(os.path.dirname(IPython.__file__), 'core/ultratb.py')

Finally, open the file and look for:
style = stack_data.style_with_executing_node(style, "bg:ansiyellow")

For the time being, you can manually patch it by changing bg:ansiyellow to something that works best given your color scheme, e.g. bg:ansired or bg:#ff0000.
Comment: I would strongly suggest using Evan Grim's answer instead. Changes to packages' python files will be lost if the package (ipython) is ever updated, or if you install ipython in a second virtual environment, etc.
Comment: Correct; added a note.
Answer: Here's an option you can drop in your ipython_config.py to duck punch in a better background color:
try:
    from IPython.core import ultratb
    ultratb.VerboseTB._tb_highlight = "bg:ansired"
except Exception:
    print("Error patching background color for tracebacks, they'll be the ugly default instead")

Verified to work with IPython version 8.7.0


Change Archon keybindings

Question: In the Wizard's Archon mode, the ability keybindings (1–4, mouse left & right clicks) are different than my normal keybindings, so switching between two setups leaves me teleporting into the crowd. I prefer my normal keybindings over Archon's, but realize that I can change my normal keybindings to match that of Archon's to minimize confusion. But is there a way to change the keybindings of Archon?
Comment: I don't believe so.
Answer: Wizards have been asking for this since launch. Still doesn't look like it's ever going to happen.
Instead, we just change our out-of-Archon bindings to closely match our in-Archon bindings. That, or get used to the different bindings between the modes.

Change Archon keybindings


how to make 2 conditions in componentDidUpdate?

Question: How to make  in "if" statement 2 conditions in componentDidUpdate. Here is my code:
componentDidUpdate(prevProps, prevState) {
    if (
      prevState.address_id !== this.state.address_id &&
      prevState.whoIsMetting !== this.state.whoIsMetting
    ) {
      console.log("hello")

    }
  }

It doesnt fire any events, but If I use them separately it works fine. I need to check for both statements at same time. Is possible to make 2 conditions in componentDidMount or is there any other  ways?
Comment: _but If I use them separately it works fine..._ well, when you check them separately then your condition is different isn't it
Comment: This will check if both `address_id` and `whoIsMetting` have changed. You might want to use `||` instead of `&&` to check if either value has changed.
Comment: @Sagivb.g I need to check them at same time , then if both true fire my function
Comment: @Caramiriel I need both states at same time , I don't need either
Comment: So the code you posted is fine
Answer: If you need them both true, you can chain them:
componentDidUpdate(prevProps, prevState) {
    if (prevState.address_id !== this.state.address_id){
           if( prevState.whoIsMetting !== this.state.whoIsMetting){
                // do stuff if both true;
           } else {
             // do stuff if first true and second false
           }
      } else {
          // do stuff if first false
      }
}

Comment: If the OP needs both conditions to be true when checking, his code should be working just fine. The answer I posted is just a making the same thing lengthier. Only thing I can think of is that the way the other components are working, his conditions are cancelling each other. Like either the first is true or the second. With my answer he can track which one.
Comment: @Dblaze47 Thanks! Thats what I want. I have mentioned couple times that I need both conditions to be true , why everyone keeps offering answers with single check or || statement.
Answer: Your problem is with the logic of your if, not the syntax. You can write any valid JavaScript inside a componentDidMount, as it is just a function like any other.
Writing two if functions like so:
if (prevState.address_id !== this.state.address_id)
...
if(prevState.whoIsMetting !== this.state.whoIsMetting)
...

Is very different from checking for both statements at the same time with an && operator. The && means both need to be true. If you want a way to check both of them and get the same result as if you were using two ifs (like above) then:
if (
      prevState.address_id !== this.state.address_id ||
      prevState.whoIsMetting !== this.state.whoIsMetting
    )

Will do the work, as you're checking one OR the other to be true.
Answer: You have used && then the condition will be true if both of them were true otherwise if you want to execute the statement if one of them is true you can use || instead.
And if your condition
prevState.address_id !== this.state.address_id
is true it will check
prevState.whoIsMetting !== this.state.whoIsMetting
but if the first condition is false it won't check the second one and it will return false directly.


Type for string with undetermined ending

Question: I have a string, it starts with some prefix. How can i define a type which correspond to something like this:
 type group_of_strings = 'custom_name ' + string

Where string is a type. So purpose of this to filter all strings which are not belong to group of strings starting with this particular prefix. Is this possible? Thank you.
Comment: maybe you can use String Enums, to be included in the next version of TypeScript: https://github.com/Microsoft/TypeScript/pull/15486 . You can test them now using typescript@next
Comment: thank you for suggestion i will check this out
Comment: how would you use string enums to solve the problem?
Answer:
Is this possible? Thank you.

No. It cannot be specified at compile time using TypeScript / FlowType / Google Clojure / Dart type systems.
Other tools might but I am familiar enough with these to give an exact answer
Alternative
You can model it as
type group_of_strings =
 {
   prefix: 'custom_name ',
   suffix: string
 }

But it wouldn't be a string literal in this case and instead be an object containing two string properties.
Answer: Yes, this can be done using Template Literal Types:
type PrefixedString = `custom_name${string}`;


Shell script to unzip a file with a password

Question: I am trying to unzip a file with a given password from the vendor. I need to automate this process so I'd like a command to do it, so that I can write a script.
Comment: Try this: `unzip  -P password file.zip`
Answer: To unzip files with password use the -P option:
unzip -P password file.zip

To create a directory with same structure as the source .zip and place contents in it:
unzip -P password file.zip

# output
total 560
drwxrwxr-x   2 george george   4096 Mar  3 14:02 ./
drwxr-xr-x 199 george george  12288 Mar  3 14:01 ../
drwx------   2 george george   4096 Apr 23  2016 file/
-rw-rw-r--   1 george george 150058 Feb 18 07:00 file.zip

To unzip content into current directory without creating one with same structure as source .zip file:
unzip -j -P password file.zip

# output
total 560
drwxrwxr-x   2 george george   4096 Mar  3 14:02 ./
drwxr-xr-x 199 george george  12288 Mar  3 14:01 ../
-rw-rw-r--   1 george george 150058 Feb 18 07:00 file.zip
-rw-------   1 george george 405115 Apr 23  2016 Transcript.pdf

Answer: If you are getting an unsupported compression method 99 error, then your ZIP file is AES encrypted which is currently not supported by unzip. Use 7zip instead (install with sudo apt install p7zip-full):
7za x -p<PASSWORD> file.zip


How to alphabetically sort a file in Python

Question: So, I've got this file that's made out of countries and some languages that are being studied there(it's all made up data) and I need to alphabetically sort the file based on 1) the names of the countries and 2) on the languages if there are more than one being studied. I tried converting it to csv and also making a list of lists out of it but nothing seems to work. Help would be appreciated :)
Each line is in this format:
[Country] [Language] [Number Of Students]
Comment: You have added the `python` tag, but you have added no code to your question.
Comment: can you provide some sample data and what you would expect the result to be
Comment: You can find some useful information here:
https://stackoverflow.com/questions/4174941/how-to-sort-a-list-of-lists-by-a-specific-index-of-the-inner-list
//
https://stackoverflow.com/questions/3368969/find-string-between-two-substrings
//
https://www.w3schools.com/python/python_file_open.asp
Answer: Here is a possible solution using Pandas:
<code>import pandas as pd

df = pd.read_csv("file.txt", names=["country", "language", "nstudents"], sep=" ")
df.sort_values(["country", "language"], inplace=True)
df.to_csv("new_file.txt", index=False, header=False, sep=" ")
</code>


Can't Pass MultipartFile through Ajax JAVA

Question: I'm trying to send a file from my jsp to controller via ajax, im getting EXCEPTION:Null Error, below is my code:
Controller:
@RequestMapping(value = "/schedulebatch",method =
RequestMethod.POST,params="insertData")
public @ResponseBody String addBatch(@RequestParam(
@RequestParam(value="upfile",required=false) MultipartFile upfile) throws
Exception {  if(!upfile.isEmpty()){
                    System.out.println("test1");}

View:

 $("#submit-btn").click(function(){

 var upfile = document.getElementById('upfile').enctypeVIEW;
    alert('test');
 if(batchname==null || fromdate == null || partners == null || interval ==
    null){
  $('#insertmodalalert').empty();
     $('#insertmodalalert').append('
NOTICE | Please fill out the required form.
');
     $('#alertMod').modal();
     $('#okbtn').click(function(){
      window.location.reload(true);
     });
 }
 else{

  $.ajax({

   type: "POST",
   url: "schedulebatch?insertData",
   data: {"upfile" : upfile},

           success: function(response){
      // alert('test');
            $('#insertmodalalert').empty();
            $('#insertmodalalert').append('
NOTICE | '+response+'
');
            $('#alertMod').modal();
            $('#okbtn').click(function(){
             $('#alertMod').modal('hide');
             window.location.reload(true);
            });
           },
           error: function(xhr, status, error) {

              var err = eval("(" + xhr.responseText + ")");
            // alert('test');
              // Display the specific error raised by the server
    (e.g. not a
              //   valid value for Int32, or attempted to divide by
    zero).
              $('#insertmodalalert').append('
NOTICE | '+err.Message+'
');
             $('#activateMod').modal();

             $('#okbtn').click(function(){
              $('#alertMod').modal('hide');
              window.location.reload(true);
             });
           }
       });
  //alert("Test");
 }

HTML:

 File to upload: <input class="form-control" type="file" name="upfile"
    accept=".csv" id="upfile">
<button type="submit" class="btn btn-success" id="submit-
   btn">Submit</button>

I narrowed down the code to the only thing that gives me error, thanks in advance. It gets the Multipart file successfully but im not sure why it gives a null exception error
Comment: As an obvious problem, you mark your upload file as `required = false` and then assume that it is present anyway.
Answer: When I uploading files with my RestController in Spring Boot I used like below and everything is fine :
@PostMapping
public ResponseEntity<User> post(UserCreateRequest request, @RequestPart("file") MultipartFile file) throws IOException {
    ftpService.uploadPhoto(file.getOriginalFilename(), file.getInputStream());

     return ResponseEntity.ok(userService.create(request));
 }

So may be you can try to change your code as below:
@RequestMapping(value = "/schedulebatch",method =
RequestMethod.POST,params="insertData")
public @ResponseBody String addBatch(@RequestPart("upfile") MultipartFile upfile) throws Exception {
    if(!upfile.isEmpty()){
        System.out.println("test1");
    }
}

And your content-type should be multipart/form-data so I added an example html&ajax form request:

        <form method="POST" enctype="multipart/form-data" id="my-form">
            <input type="file" name="upfile"/>

            <input type="submit" value="Submit" id="btnSubmit"/>

        </form>

Comment: I tried this and i got this error " EXCEPTION: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]: Specified class is an interface" :(
Comment: You must send it as multipart/form-data in your ajax request
Comment: I updated the answer for your ajax request
Comment: tried this but im still getting " EXCEPTION: Required MultipartFile parameter 'upfile' is not present"
Comment: You should change form input file name as "upfile"
Comment: The input type is not inside a form, here is my input type   im trying to do pass the csv file to my controller without using form and by ajax
Comment: Why don't you wrap your input with form as shown above? And you can still post it via ajax shown as in my example.
Comment: It worked!! i was trying to avoid form because this ajax that im asking is an ajax that is being executed from another ajax, and that ajax is being executed by a submit-btn event which is not inside a form. SO now i was able to execute 2 controllers with 1 submit button. Thanks a lot!
Comment: I'm glad to helps you


Solar backup system, advice needed

Question: Here in South Africa we are experiencing frequent power blackouts, as I work from home on a small property (generators not an option) this is rather disruptive to my work as a programmer. So bearing this and the rapidly rising electricity prices in mind I need to find a solution.
I've searched already and found very similar questions, but most of them are centered on e.g. the US and give advice that won't work for here.
A few key differences.
1) There are no rebates here for installing solar
2) There is no 'grid tie' allowed - that is feeding back into the grid is not only not paid for but is illegal.
3) The blackouts - we experience and probably will for the next few years 'rolling blackouts' of 2 - 4 hours at various time intervals - almost always during daylight hours - due to power shortages.
Also solar and especially batteries are very expensive here and I definitely can't afford a complete off grid system up front, and would probably want to buy any system I do get in pieces.
So here are my needs in order of priority:
1 - I need to get my critical systems for work: computers/routers etc. onto battery power with enough juice to last at least 2 hours ASAP the solar portion can be added on later.
2 - I would like to power at least that part of the system with solar, this will help offset my power costs and also increase the length of power outage I can go through should things get worse.
3 - I would like to be able to use any excess power to offset the costs of the rest of my household items, and possibly add more solar in future to cover more of it, but only want to generate around 80% as any excess generation would go to waste (can't feed back into grid) - and I don't want to waste money on batteries I don't need.
Below is roughly what I think a system that best fits my situation is like.

So a few questions:
1- Any recommendations on where you would change/improve/modify this system to get better value for money, improve safety, improve efficiency or otherwise improve on it somehow?
2 - How to stop the feedback into grid?
3 - What can I use for the 'splitter' portion to ensure that power goes first to the critical system and that only excess goes to the non critical stuff?
4 - At the 'combine' portion, how do I ensure that the non-grid power is favoured first before the grid power (when both are available)
5 - How can I ensure that charge controller only takes from the AC if batteries are below a certain level, do some charge controllers already have this feature or do I need an other component for that?
6 - Given that routers, computers etc. all run DC internally would it be worthwhile looking at having them all draw directly from the battery to avoid the cost and efficiency loss of the inverter - or is it not worth the hassle?
Comment: A UPS of that sort of battery capacity, especially one that is capable of having capacity expanded further in future, is incredibly expensive here.
Also I don't want to limit my ability to go off grid completely in future so separate batteries and inverters make more sense to me - its essentially what a UPS is anyway except split up into seperate components.

I do already have a basic UPS on which the equipment runs now but I've already pushed it to its limits by upgrading it to last for 1 hour.
Comment: That seems very complex. Is a large UPS unsuitable as a first step?
Comment: http://www.bbc.co.uk/news/technology-32545081
Comment: The Tesla home battery is just a more expensive version of what is already out there, while it has some benefits over the competition and may be an exciting development for people living in USA I can't really see the relevance here.
Answer: I think there are two levels of Battery backup here, First you can do a full move into Solar. in which case you would need a LOT of changes to your current setup (energy efficient devices, LED Light Bulbs, Solar Geyser, LED PC Screens, LED TV, Alternatives to tumble-dryer, gas stove etc. )  I dont think its as easy as spending lots of money on a solar implementation.
In terms of your PC Issue, a nice, relatively cheap option would be to only backup your pc equipment. I am in the process of doing that with my Modem, Router, Switches (all 9v). Laptop and two Screens (LED 19v). I will use a simple DC Charge controller and Fail-over circuit (HERE) and then will have a LION batterypack made up specifically for the two 19v screens, The laptop will just get an extra-size battery
Otherwise the alternative is just to go the route of a large UPS, But I am trying to use source voltage current to avoid the need for a pure sine wave converter (since my pc and monitors are really sensitive to different wave patterns) - for that reason, I would like to get everything on DC...
This is not an implemented solution, but I am in the process of designing (also in RSA). so a good (cheap) solution would be great
Comment: The idea of course would be to slowly move to a full solar setup so not immediately, I am replacing my lights as they blow with LEDs etc.

So I would definitely be following some kind of multi step plan here.
Step 1 - Get my computers onto some kind of battery backup system that charges from AC.
Step 2 - Supplement that battery system with solar.
Step 3 - Increase solar to also cover some of the rest of my house.
Comment: In terms of step 1 - just the computer setup - it seems inefficient that in its final form with solar it would be something like this with a UPS.

Solar (DC)->Batteries (DC)->Inverter (AC)->UPS (AC)->UPS battery (DC)->UPS inverter (AC)->Computer/Router/Switch (DC)
The inefficiency there seems staggering to me, which means more expensive unnecessary batteries, more expensive panels etc. So a UPS doesn't seem a good step if I plan to later go solar. I suppose one could split the array but I still don't really like the idea.
Comment: Even without a UPS it is still:
Solar (DC)->Batteries (DC)->Inverter (AC)->Computer/Router/Switch (DC)

This seems wasteful to me, if I could skip the inverter and feed everything DC straight it seems I could spend less on batteries and panels in the end.
One way is to buy e.g. 3 of http://tinyurl.com/qacvjdv for the computers, but all my other devices are on different voltages so I'd need something similar for those.
I wonder if there isn't some better way to provide multiple DC voltages that everything can feed off directly, there are probably about 5/6 voltages I would need in total.
Comment: I agree, the inverter is wasteful. my setup would be 24V Battery bank (2x100ah solar) attached to a charge controller with failover, then Dc-DC converter chips to drop to the 19v, 9v and 12v needed by my devices. That system will run permanently and supplement my critical devices. http://tyconpower.com/products/files/TP-SCPOE-2424-HP_spec_sheet.pdf
Comment: Good to run into another RSA sufferer by the way :p

Thanks thats helpful, it seems a split charge controller like that is just what I need for the first step, will just need to find one with higher wattage and not PoW preferably.
Will go do some homework on these.

Have you shopped around on these yet? Any reasonably priced local suppliers etc.?
Answer: With grid-tie being illegal, there's really not a way to do what you have diagrammed, as far as I know, and I spent several years working towards a large and complex off-grid system before local conditions changed and it became practical for me to grid-connect. In particular, the "draw from solar, supplement from grid" (or questions 2 & 4) part does not work - for non-grid-tie it has to be one or the other with a transfer switch, it can't be both (well it can be both if you double-convert, but all the AC would come from the inverter - and you'd have efficiency loss X2 for the double conversion.)
My thoughts on this type of system lead me towards the following general idea, which I may still implement alongside my grid system.

Pick a system voltage - I'm leaning towards 24.
Build battery bank - despite various fancy options, "golf cart
batteries" (~225 AH/6v) seem to win the "value for money" and WIDELY
available categories.
Do not neglect adequate DC-rated fuses on the bank.
Attach a good grid charger. A bad charger will murder batteries (as a UPS owner you may already know this too well - many UPSes seem to have terrible chargers and terrible battery life as a result.) One possible approach is to use a very basic (but preferably efficient) AC-DC supply and use that as input to an "offgrid" charge controller; then you'll have the charge controller you'd eventually need for your solar panels, and they are normally excellent battery chargers.
Use DC-DC converters to get other DC voltages you need as
directly/efficiently as possible. i.e. Question 6 - yes, I think it's worthwhile where at all practical. The convenience of AC comes at a considerable efficiency cost.
Add a charge controller (if you are not using one as a battery charger) and solar panels when possible.
If the battery bank/solar capacity seems adequate, add an inverter
for AC loads.

Control your use of inverter .vs. grid power by choosing where to plug loads in, or with a transfer switch. Plug control leaves the control system in your head and is low-cost.

Solar backup system, advice needed