Count frequency of item in a list of tuples

Question: I have a list of tuples as shown below. I have to count how many items have a number greater than 1. The code that I have written so far is very slow. Even if there are around 10K tuples, if you see below example string appears two times, so i have to get such kind of strings. My question is what is the best way to achieve the count of strings here by iterating over the generator
List:
 b_data=[('example',123),('example-one',456),('example',987),.....]

My code so far:
blockslst=[]
for line in b_data:
    blockslst.append(line[0])

blocklstgtone=[]
for item in blockslst:
    if(blockslst.count(item)>1):
        blocklstgtone.append(item)

Comment: By the way, that isn't a generator expression, it's a list.
Answer: You've got the right idea extracting the first item from each tuple. You can make your code more concise using a list/generator comprehension, as I show you below.
From that point on, the most idiomatic manner to find frequency counts of elements is using a collections.Counter object.

Extract the first elements from your list of tuples (using a comprehension)
Pass this to Counter
Query count of example

from collections import Counter

counts = Counter(x[0] for x in b_data)
print(counts['example'])

Sure, you can use list.count if it's only one item you want to find frequency counts for, but in the general case, a Counter is the way to go.

The advantage of a Counter is it performs frequency counts of all elements (not just example) in linear (O(N)) time. Say you also wanted to query the count of another element, say foo. That would be done with -
print(counts['foo'])

If 'foo' doesn't exist in the list, 0 is returned.
If you want to find the most common elements, call counts.most_common -
print(counts.most_common(n))

Where n is the number of elements you want to display. If you want to see everything, don't pass n.

To retrieve counts of most common elements, one efficient way to do this is to query most_common and then extract all elements with counts over 1, efficiently with itertools.
from itertools import takewhile

l = [1, 1, 2, 2, 3, 3, 1, 1, 5, 4, 6, 7, 7, 8, 3, 3, 2, 1]
c = Counter(l)

list(takewhile(lambda x: x[-1] > 1, c.most_common()))
[(1, 5), (3, 4), (2, 3), (7, 2)]

(OP edit) Alternatively, use a list comprehension to get a list of items having count > 1 -
[item[0] for item in counts.most_common() if item[-1] > 1]

Keep in mind that this isn't as efficient as the itertools.takewhile solution. For example, if you have one item with count > 1, and a million items with count equal to 1, you'd end up iterating over the list a million and one times, when you don't have to (because most_common returns frequency counts in descending order). With takewhile that isn't the case, because you stop iterating as soon as the condition of count > 1 becomes false.
Comment: Thanks for `Counter` - did not know that one and recreated it the same as Ayodhyankit Paul did
Comment: Most common method, is there way it can give me all the strings like example having count > 1
Comment: @min2bro Yes, why not. query `most_common`, and iterate over with a loop. Let me write up a small answer.
Comment: @coldspeed, Added the list comprehension to only get the list of Strings having count greater than 1
Comment: @cs95 I have a question about `list(takewhile(lambda x: x[-1] > 1, c.most_common()))`...How do you reset the counter when inside a for loop? For the life of me I can't reset it. I have tried c.clear(), c.update('a'), c = Counter()..But nothing resets the counter. It just keeps adding and adding...
Answer: First method :

What about without loop ?

print(list(map(lambda x:x[0],b_data)).count('example'))

output:
2

Second method :
You can calculate using simple dict , without importing any external module or without making it so complex:
b_data = [('example', 123), ('example-one', 456), ('example', 987)]

dict_1={}
for i in b_data:
    if i[0] not in dict_1:
        dict_1[i[0]]=1
    else:
        dict_1[i[0]]+=1

print(dict_1)

print(list(filter(lambda y:y!=None,(map(lambda x:(x,dict_1.get(x)) if dict_1.get(x)>1 else None,dict_1.keys())))))

output:
[('example', 2)]

Test_case :

b_data = [('example', 123), ('example-one', 456), ('example', 987),('example-one', 456),('example-one', 456),('example-two', 456),('example-two', 456),('example-two', 456),('example-two', 456)]

output:
[('example-two', 4), ('example-one', 3), ('example', 2)]

Comment: @min2bro if you want for all string then go with my second solution , and yeah i fixed that issue now you don't have to iterate over dic_t dictionary to find the strings having count greater than 1 check .
Comment: The only problem here is that I need to again iterate over the dict_1 dictionary to find the strings having count greater than 1
Comment: @min2bro i have update the  solution now check , you even don't need loop there or any external module.
Comment: I am looking for all the strings which have count > 1, so the above solution works for one string count at a time
Comment: Your code is just a re-implementation of `Counter`. Why are you reinventing the wheel?
Comment: @cᴏʟᴅsᴘᴇᴇᴅ i don't know about `Counter`  , never use that ,  I just wrote my own logic .
Answer:
Time it took me to do this
  ayodhyankit-paul
  posted the same - leaving it in non the less for the generator code
  for testcases and timing:

Creating 100001 items took roughly 5 seconds, counting took about 0.3s,
filtering on counts was too fast to measure (with datetime.now() - did not bother with perf_counter) - all in all it took less then 5.1s from start to finish for about 10 times the data you operate on.
I think this similar to what Counter in COLDSPEEDs answer does:
foreach item in list of tuples:

if item[0] not in list, put into dict with count of 1
else increment count in dict by 1

Code:
from collections import Counter
import random
from datetime import datetime # good enough for a loong running op

dt_datagen = datetime.now()
numberOfKeys = 100000

# basis for testdata
textData = ["example", "pose", "text","someone"]
numData = [random.randint(100,1000) for _ in range(1,10)] # irrelevant

# create random testdata from above lists
tData = [(random.choice(textData)+str(a%10),random.choice(numData)) for a in range(numberOfKeys)]

tData.append(("aaa",99))

dt_dictioning = datetime.now()

# create a dict
countEm = {}

# put all your data into dict, counting them
for p in tData:
    if p[0] in countEm:
        countEm[p[0]] += 1
    else:
        countEm[p[0]] = 1

dt_filtering = datetime.now()
#comparison result-wise (commented out)
#counts = Counter(x[0] for x in tData)
#for c in sorted(counts):
#    print(c, " = ", counts[c])
#print()
# output dict if count > 1
subList = [x for x in countEm if countEm[x] > 1] # without "aaa"

dt_printing = datetime.now()

for c in sorted(subList):
    if (countEm[c] > 1):
        print(c, " = ", countEm[c])

dt_end = datetime.now()

print( "\n\nCreating ", len(tData) , " testdataitems took:\t", (dt_dictioning-dt_datagen).total_seconds(), " seconds")
print( "Putting them into dictionary took \t", (dt_filtering-dt_dictioning).total_seconds(), " seconds")
print( "Filtering donw to those > 1 hits took \t", (dt_printing-dt_filtering).total_seconds(), " seconds")
print( "Printing all the items left took    \t", (dt_end-dt_printing).total_seconds(), " seconds")

print( "\nTotal time: \t", (dt_end- dt_datagen).total_seconds(), " seconds" )

Output:
# reformatted for bevity
example0  =  2520       example1  =  2535       example2  =  2415
example3  =  2511       example4  =  2511       example5  =  2444
example6  =  2517       example7  =  2467       example8  =  2482
example9  =  2501

pose0  =  2528          pose1  =  2449          pose2  =  2520
pose3  =  2503          pose4  =  2531          pose5  =  2546
pose6  =  2511          pose7  =  2452          pose8  =  2538
pose9  =  2554

someone0  =  2498       someone1  =  2521       someone2  =  2527
someone3  =  2456       someone4  =  2399       someone5  =  2487
someone6  =  2463       someone7  =  2589       someone8  =  2404
someone9  =  2543

text0  =  2454          text1  =  2495          text2  =  2538
text3  =  2530          text4  =  2559          text5  =  2523
text6  =  2509          text7  =  2492          text8  =  2576
text9  =  2402

Creating  100001  testdataitems took:    4.728604  seconds
Putting them into dictionary took        0.273245  seconds
Filtering donw to those > 1 hits took    0.0  seconds
Printing all the items left took         0.031234  seconds

Total time:      5.033083  seconds

Comment: I can still appreciate a well put together answer. Upvoted, cheers.
Comment: @COOLDSPEED mentions in other answer that this is about what Counter does internally - so dont use mine, use `Counter` ;) my guess would be it works even smarter.
Answer: Let me give you an example to make you understand.Although this example is very much different than your example, I found it very helpful while solving these type of questions.
from collections import Counter

a = [
(0, "Hadoop"), (0, "Big Data"), (0, "HBase"), (0, "Java"),
(1, "Postgres"), (2, "Python"), (2, "scikit-learn"), (2, "scipy"),
(2, "numpy"), (2, "statsmodels"), (2, "pandas"), (3, "R"), (3, "Python"),
(3, "statistics"), (3, "regression"), (3, "probability"),
(4, "machine learning"), (4, "regression"), (4, "decision trees"),
(4, "libsvm"), (5, "Python"), (5, "R"), (5, "Java"), (5, "C++"),
(5, "Haskell"), (5, "programming languages"), (6, "statistics"),
(6, "probability"), (6, "mathematics"), (6, "theory"),
(7, "machine learning"), (7, "scikit-learn"), (7, "Mahout"),
(7, "neural networks"), (8, "neural networks"), (8, "deep learning"),
(8, "Big Data"), (8, "artificial intelligence"), (9, "Hadoop"),
(9, "Java"), (9, "MapReduce"), (9, "Big Data")
]
#
# 1. Lowercase everything
# 2. Split it into words.
# 3. Count the results.

dictionary = Counter(word for i, j in a for word in j.lower().split())

print(dictionary)

# print out every words if the count > 1
[print(word, count) for word, count in dictionary.most_common() if count > 1]

Now this is your example solved in the above manner
from collections import Counter
a=[('example',123),('example-one',456),('example',987),('example2',987),('example3',987)]

dict = Counter(word for i,j in a for word in i.lower().split() )

print(dict)

[print(word ,count) for word,count in dict.most_common() if count > 1  ]


How to restrict the length of the html anchor tag text?

Question: I want to link a certain text which is too long. For instance, assume the text is:
"Link this text to a page that will have the corresponding result set. The result set page inturn would redirect to another page that would give individual item. The individual item would then redirect back to the original link".
I can do that by placing the above text between  ...  tags. Things are fine... Life is cool. Now comes the tough part. The above text would be displayed on a single line increasing the page size horizontally. I want to split the string while maintaining the anchor link for that. For instance I want some thing like this:
Link this text to a page that will have the corresponding result set.
The result set page inturn would redirect to another page that would give individualitem.
The result set page inturn would redirect to another page that would give individual item.
However, these three lines should be considered as a single link and hovering the mouse over any of these lines would however display the underline for all the three lines (i.e. each of the line should not be considered individual link).
Is this possible??
Comment: Anchor elements do not increase the page horizontally. They wrap automatically.
Comment: And if you want the lines to break at specific places, just add `` (stands for "break") to force it. The link will still be one link as long as you only use one set of `...`.
Answer: It's just a simple matter of nesting HTML tags properly.  For example, to have those three lines all separate but the same link, place them in individual
 tags but encapsulate the entire segment in a  tag.

This is the first line.

Another line.

Down at the bottom, same link.

Answer: If you want to split the string into sentences, you'll have to do some string manipulation.
A better idea would probably be to put the text in a container and give the container a fixed width.
Answer: not sure if it's standards compliant but you can do this:
http://jsfiddle.net/Nf2TW/
Link this text to a page that will have the corresponding result set.

The result set page inturn would redirect to another page that would give individualitem.

The result set page inturn would redirect to another page that would give individual item.

Comment: As of HTML5, this is standards compliant. http://html5doctor.com/block-level-links-in-html-5/
Comment: And as of like 10 years ago this is how every browser rendered this anyway =).
Answer:
The above text would be displayed on a single line increasing the page size horizontally.

No it wouldn't. The browser should wrap that text to the size of the container. Just like it will right here:
Link this text to a page that will have the corresponding result set. The result set page inturn would redirect to another page that would give individual item. The individual item would then redirect back to the original link
Answer: I would do something like this...

bla bla bla bla bla bla bla

This should make the div a link but also limit the spacing. If you want to break it up by sentence or something I would just place a
 tag where you would like it to go to the next line (do not use
 as this is not web standard).


How to texturize a 3D model from video?

Question: I have a base 3D mesh of an object that I want to texturize. I also have a 360 degrees video of the real-world object.
What are good ways to use the video to texturize the mesh? Are there existing tools to do that? (paid or free)
Edit: I have complete control over the camera and object positions, I can measure the distances and whatever needed. I can also put markers on the object.
Comment: @MichaelLitvin I think it would be off-topic if you ask "How to do in a specific software" on this site.
Comment: Most 3d apps can do this.
Comment: Could you add some details?If you could explain the general process in Blender (or any other app) that would be great.
Comment: I'm not asking about just Blender, though a suggestion for how to do this in Blender would be nice.
Answer: One method is to do projection mapping. I understand that most 3D rendering applications can do that. I don't have much experience with them, but I have done it using AfterEffects using the Camera Mapper in Buena Depth Cue.
Algorithmically, it's achieved by projecting the geometry back onto the image to generate texture coordinates. So one way to do that is as follows:

Line up the image of the scene in front of the corresponding geometry
For each triangle in your geometry, project the 3 corners onto the image
The coordinates where the projection hits your image are the texture coordinates you need for that triangle
Comment: This is a solution for a single image or frame, not the while video. To use this, the texture maps for many frames will need to be stitched somehow... This is the main problem here.
Comment: unknown @MichaelLitwin they dont need to be stitched, just masked. Its pretty easy all you do is you calculateuseful  how much the pixel area is facing the camera tha when the facing drops under a certain treshold mask it out. Then median filter the overlap to remove noise. Then either bake to 2D or just use the projection as is. Any projection works just as long as you can track the projector location in 3D
Comment: @MichaelLitvin you dont need to you can simply let them overlap.
Comment: Will this work with perspective projection ?
Comment: I don't understand - I'm using several images for texture. These images will meet somewhere, and edge lines won't necessarily meet at exactly the same place. How do I ensure they do?
Answer: If I understand you correctly, you have a 3D model, and a video of the real world object? This sounds like a job for photogrammetry. Using a photogrammetry plugin, you can derive a model of what is seen in the video, and a texture map of it. The derived model probably won't be exactly like your 3D model, but you may be able to remap the texture map back onto your 3D model. Doing a search on photogrammetry and blender should give you some good links.

How to texturize a 3D model from video?


wp-env docker issue connecting to mysql

Question: I get a db error when i run wp-env logs..
$ wp-env logs
wordpress_1        |
wordpress_1        | MySQL Connection Error: (2006) MySQL server has gone away
wordpress_1        | [25-Oct-2020 12:12:33 UTC] PHP Warning:  Packets out of order. Expected 0 received 1. Packet size=71 in Standard input code on line 22
wordpress_1        | [25-Oct-2020 12:12:33 UTC] PHP Warning:  mysqli::__construct(): Error while reading greeting packet. PID=70 in Standard input code on line 22
wordpress_1        | [25-Oct-2020 12:12:33 UTC] PHP Warning:  mysqli::__construct(): (HY000/2006): MySQL server has gone away in Standard input code on line 22

so I reset MySQLd with some new variables in /etc/mysql/my.cnf
[mysqld]                                                                                |
# 8 hours                                                                                |
wait_timeout = 28800                                                                           |
# 8 hours                                                                                |
interactive_timeout = 28800                                                                    |
max_allowed_packet = 256M

then restart mysql
service mysql restart

but that doesn't seem to fix it. I don't have a lot of mem on this computer. could that be an issue ... ?
# free -h
total        used        free      shared  buff/cache   available
|Mem:           1.9G        1.2G        129M        173M        534M        339M
#                                                                                |Swap:          1.3G        1.2G         97M

does anyone have any ideas ?
this is my .wp-env.json file ...
{
"core": ".",
"themes": [
    "./wp-content/themes/s-child"
],
"config": {
    "WP_DB_HOST":"127.0.0.1"
    }
}

Answer: So basically I included
bind-address = 0.0.0.0

in /etc/mysql/my.cnf above. It seems that the docker container can't connect to MySQL on localhost by design. This allows mysqld to connect to the docker container on any IP.
Then make sure that your MySQL root user can also connect from non-localhost IP's...
Login to MySQL from the Linux cli ...  Check if root is available from '%' (any IP)...
mysql> SELECT host, user, password FROM user WHERE host = '%';
+------------------+------+-------------------------------------------+
| host             | user | password                                  |
+------------------+------+-------------------------------------------+
| %                | root | *xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+------------------+------+-------------------------------------------+

If you don't see above then run to allow all access ...
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';

Then restart MySQL ..
service mysql restart

Then check to see where the MySQL service is bound ..
dara@dara-HP-Stream-Laptop-11-y0XX:~$ sudo netstat -tupan | grep mysql
[sudo] password for dara:
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      916/mysqld

Or since MySQL is configured to run on localhost only by default. You could comment out the following in my.cnf
# bind-address = 127.0.0.1

Which takes care of the MySql connection error issue.
Then since I'm running a wp-env docker container to create a Wordpress image. I run a
wp-env clean all

Which cleans the Wordpress databases ... then I do a
wp-env --update --debug start

To create a Wordpress docker container with access to MySQL
If that doesn't work, then try a
wp-env destroy && wp-env start

Warning: wp-env destroy is the last resort and deletes docker containers, volumes, and networks associated with the WordPress environment and removes local files.
However, I had no docker containers, volumes or networks i wanted to preserve. This worked for me.

wp-env docker issue connecting to mysql


How to get refference to the unmanaged (not COM) interface in C#?

Question: I have a native DLL which is implementing some API. The C++ header looks like this:
class CAPIInterface
{
public:
    virtual int    __stdcall Release()=0;
    virtual LPCSTR __stdcall ErrorDescription(const int code)=0;
    virtual int  __stdcall Login(const int login,LPCSTR password)=0;
}

In C++ a pointer to the interface is acquired this way:
typedef int (*APICreate_t)(int version,CAPIInterface **api);

pfnAPICreate =reinterpret_cast<APICreate_t>(::GetProcAddress(hlib,"APICreate"));
CAPIInterface *api=NULL;
if(pfnAPICreate) (*pfnAPICreate)(version,&api);

The methods of the interface are called like this:
api->Login(123,"password");

Now I need to load this native DLL and use the API in my C# program. I managed to load the DLL and acquire the pointer to the native interface this way:
    public static class GlobalMembers
    {
        [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public delegate int APICreate_t(time_t version, out IntPtr api);
    }
    ptr_pfnAPICreate = NativeMethods.GetProcAddress(hlib,"APICreate");
    pfnAPICreate = (GlobalMembers.APICreate_t)Marshal.GetDelegateForFunctionPointer(ptr_pfnAPICreate, typeof(GlobalMembers.APICreate_t));
    pfnAPICreate(version, out mptr);

But now I'm not sure how to map this pointer (mptr) to the C# implementation of the interface. Also I'm not sure how to declare the interface CAPIInterface in C# as well. I tried declaring the interface this way:
[StructLayout(LayoutKind.Sequential)]
public class CAPIInterface
{
    public delegate int Release();
    public delegate string ErrorDescription(int code);
    public delegate int Login(int login, string password);
}

But then it doesn't compile... it returns this error:
Error   3   Non-invocable member 'CAPIInterface.Login' cannot be used like a method.    I understand that the delegates must be instantiated somewhere as well... but how to do it? Is it correct approach at all to declare the CAPIInterface as above?
Comment: You should try something like http://www.swig.org/Doc1.3/CSharp.html that makes it easier
Comment: thanks, but I would prefer calling methods of the native DLL directly from my C# application if possible. Can you give me some recommendations how to do it in my code?
Comment: I have no clue on how to do this, but I was wondering if it would not be easier to make your C++ library COM visible and make C# use the library using COM instead.
Comment: Mapping a C++ class to C# is not for the faint hearted unless you are using a strict COM interface. The simple thing to do would be either to use SWIG or store IntPtrs for your C++ objects and use extern C functions that take your C++ class pointer as the first argument
Comment: I never used SWIG before... can you tell me how to convert my C++ API header with SWIG?
Comment: I did this: unpacked SWIG, created a test project in ...\swigwin-2.0.9\Examples\csharp\ folder, added my API.h to the project, created API.i file and included API.h in it, added API.i file to the project as well. When I try to compile the project it says: Error 2 error LNK2001: unresolved external symbol __DllMainCRTStartup@12 c:\Programs\swigwin-2.0.9\swigwin-2.0.9\Examples\csharp\API\LINK
Answer: I was able to convert my C++ API to C# with the help of SWIG. It works great. Thank you.
Comment: can you explain how you did it?


Is this switch properly grounded?

Question: I replaced a standard single pole switch with an Intermatic ST01 timer switch, into a metal electrical box with three other switches. The switch I was replacing had no ground connection, but some of the others do.
When installing the timer switch, I attached the green ground wire from the switch to a screw at the back of the electrical box. This is my first time doing this, so I wanted to find out if this is properly/safely grounded, or if I missed something.
Comment: Is there room in that box for the timer switch with all those wires and wire nuts behind it? The specs for the ST01 series say that it is quite deep (i.e. 2.75 in. H x 1.73 in. W x 1.63 in.D).
Answer: That screw looks like it's for the wire clamp that secures wiring going through the back of the junction box. Instead, you should connect the green wire with a wire nut to the exposed copper wires that I believe are visible in the bottom of the picture. Make sure to use the proper size wire nut, which is based on the wire size and number of wires being joined together.
Comment: With a metal box, you'd ground the box, but you don't use the box to ground your switch. All you're doing is adding another wire to the grounding bundle, just like all the neutrals are likely bundled together in other places (you may or may not see that inside this box depending on the power source).
Comment: Yeah, I think I may be in a little over my head (and skill level). I am going to call an electrician tomorrow to come re-do it properly.
Comment: BMitch is absolutely right. The green wire needs to be connected to the existing bare grounds, and at least one bare ground from the ground bundle bonded to the metal box using one of the green screws.
Comment: Those copper wires are already connected to other things in the box. There is a hole in the back of the box (obscured by the wires), without a screw. Is that where a grounding screw goes? Would using that be safer?
Comment: It doesn't look like much of the wire is under the screw either
Answer: Each switch should be grounded with bare copper wires coming from the green screws on the switches, to a bare copper wire screwed onto one of the wire clamp screws of the metal box, to the bare copper wires of all incoming cables.
All of these wires should be interconnected in a sound and professionally clean way.
To test that everything is properly grounded you can use a standard multimeter tool to test resistance or continuity between a grounded source and the neutral wire feeding the box.

You can set your voltimeter to continuity or to a low resistance setting as shown above.  Touching one end to neutral and another to ground should show near zero resistance on the display because the neutral and ground bars are connected at the breaker box.  Near zero resistance means that they are continuous.
A high resistance number or a display of 1 essentially means that there is infinite resistance between ground and neutral, and that means that the receptacle or switch you are testing is not grounded.

Put one end on the green screw of one of the switches and the other on the neutral to test each switch.
Next place one end on the metal box itself and the other to the neutral wire to verify the box is grounded.
Next test all the other ground wires from all other cables to verify that they are all grounded as well.

You will be able to perform this test safely with the breaker(s) shut off as you won't need a live power source to test continuity.
Comment: The white wires can be the switched hots going back to the fixture where the line may be located. The presence of a red wire makes me think you're right, but I just can't be sure. Maybe I haven't had enough sleep, but the first paragraph is confusing even if the advice is good. It reads like you're suggesting they wire the switch to the box and then to the line.
Comment: Depending on the wiring, you may not have a neutral at a switch to test continuity. And from the first paragraph, it's not clear whether or not you're suggesting the OP wire the switch directly to the box or to ground wiring bundle.
Comment: @BMitch I can tell the load is to the box because you can see the white neutral wires in the picture.  On your other point, I am saying that the ground wire bundle should also contain a ground wire that is clamped to the box, as it is a metal box and should be grounded as well.

Is this switch properly grounded?


Quantified CNF: is not this a proof that NP = PSPACE?

Question: If boolean formula $f$ is in CNF it is known that:$$\forall x \exists y_1 \exists y_2 .. \exists y_n : f(x, y_1, y_2..y_n) :\iff \exists y_1 \exists y_2 .. \exists y_n : f(0, y_1, y_2..y_n) \land f(1, y_1, y_2..y_n) :\iff \exists y_1 \exists y_2 .. \exists y_n : f(y_1, y_2..y_n) :\iff f(y_1, y_2..y_n)$$
Last formula is non-quantified CNF. For multiple $\forall$-quantified variables same reduction is also possible. SAT is NP problem, QSAT is PSPACE-complete (even in CNF). Does this involve that NP = PSPACE?
Answer: Each of your purported equivalences is problematic:
In the first one, you seem to be moving $\forall x$ willy-nilly inside the $\exists$s. This does not preserve meaning:
$$ \forall x\exists y (x=y) \qquad\text{and}\qquad \exists y(0=y\land 1=y) $$
are not equivalent -- the first one is obviously true and the second one just as obviously false.
In the second one, $f$ inexplicably changes from having $n+1$ parameters to having only $n$. What is going on there? Is it even the same $f$?
In the third one, where do the quantifiers suddenly go? You seem to be asserting that the formula $\exists y_1\cdots \exists y_n(f(y_1,\ldots,y_n))$ which has no free variables is equivalent to $f(y_1,\ldots,y_n)$ which has $n$ of them. Unless you imagine that the truth value of $f$ does not depend on its variables at all (which you don't seem to have any reason to), this can't be true.

Finally, your plan seems to be to prove that PSPACE${}\subseteq{}$NP by reducing QSAT to SAT. However, even if your equivalences worked, if you have $n$ universal quantifiers repeating your first rewrite for each of them would blow up the formula by a factor of $2^n$, so it is not a polynomial-time reduction and would not say anything about QSAT being in NP.
Comment: **First**. (x = y) is not in CNF. It's CNF: (not x + y) (x + not y). With my rule it is y + not y = 0. **Second**. They are not the same but equisatisfiable. **Third**. SAT problem is TQBF with all existence quantifiers. **Finally**. Remove variables is linear time task.
Comment: I can't understand why ∀x∃y (x = y) is true. Does it differ from ∃y∀x (x = y)?
Comment: Now I understood. (x = y) has solution 1001. First half of formula (10) is satisfiable, second (01) too, so, TQBF is true.


How to get full category path from a keyword in Tridion

Question: Can any one help me to get full category path from a given keyword. I am giving one example as below,
Example:
Category 1----> Keyword 1 -----> Keyword 11,
say from metadata i got the value "Keyword 11", but i need whole path i.e. /Category 1/ Keyword 1/Keyword 11.
Can anyone help me how to achieve this in Template Building Block using c#.
Comment: Please consider committing to Tridion Area51 proposal to get a dedicated Q&A site for SDL Tridion at http://area51.stackexchange.com/proposals/38335/tridion?referrer=eo63snjNlUWNn9xqeeO2NA2
Answer: Maybe you can try and play with one of the following:

keyword.ParentKeywords recursively to create the path you are looking for.
OrganizationalItem oi = keyword.OrganizationalItem; // to get all the organizational items
keyword.OwningRepository

Hope that helps!
Comment: in your TBB: `KeywordField kwdField = (KeywordField)metaFields["kwdField"];` and `kwdField.Value` or `Values` if it is multiple.
Comment: Could you please provide some code snippet to how to get Keyword object from value " Keyword 11" ?
Answer: Below code should help you to get the path.
<code>bool isRecursive = false;
KeywordField kwdField = (KeywordField)metaFields["kwdField"];
Keyword curKwd = new Keyword(kwdField.Value.Id, engine.GetSession());
string kwdPath = curKwd.Title;
while (!isRecursive) {
  if (curKwd.ParentKeywords.Count > 0){
     foreach (Keyword kwd in curKwd.ParentKeywords) {
        kwdPath = kwd.Title + "/" + kwdPath;
     }
     curKwd = curKwd.ParentKeywords[0];
  } else {
     isRecursive = true;
  }
}
kwdPath = curKwd.OrganizationalItem.Title + "/" + kwdPath;
</code>
Comment: depending on your version of .Net (3.5+) you could use Linq:
String.Join("/", myKeywordField.Value.ParentKeywords.Select(kw => kw.Title).ToArray());
Comment: One keyword is associated as Metadata of a component. I have Component object and Component.Metadata["keyword_value"] returns XMLElement. In this way i am not able to retrieve  "KeywordField" object. Could you please provide solution for this?
Comment: It will be same as how you read the Content fields from Component. This gives Metadata fields `ItemFields metaFields = new ItemFields(component.Metadata, component.MetadataSchema);`


To enable wireless on Thinkpad t440s

Question: 2 weeks after I bought t440s with Windows 8 bundled, I'm freaked out with it and want to replace with Precise. Using USB boot to make sure if it's run although the device looks like already certified by Canonical, Ubuntu can't find wireless device. I've found a workaround for a similar issue, I still don't know what to do.
The same URL from above says:
Network
    Intel Corporation Lynx Point-LP PCI Express Root Port 6
    Ericsson Business Mobile Networks BV Unknown

Also,
$ lsmod|grep intel
:
snd_hda_intel
kvm_intel
aesni_intel
ghash_clmuni_intel, aesni_intel
intel_ips

I'm not an expert at all with devices...I'd appreciate solution. Thanks!

Update) I'm required to use Ubuntu 12.04.

Update-2) I upgraded to 13.04, hopelessly expecting that doing so would just resolve the issue. Ended up seeing no improvement. I found the model of wireless module is Intel Wireless-N 7260 from this page.
Answer: Try going to the computer manufacter's web site and look for the wireless drivers for Windows and use windows wireless drivers from the Ubuntu web store. I hope that works, I have never had any wi-fi issues, so if this doesn't work, I'm sorry.
Comment: Good to know about `Windows Wireless Drivers`. Now I'm looking for the way to use it on USB boot (I want to verify if Ubuntu works on this machine first by USB)..
Comment: When you made the bootable USB did you set it to preserve space? If so then you can install windows wireless drivers on the USB. If you didn't I would suggest trying again on putting Ubuntu on the USB and preserve some space.
Answer: Answering my own question since I finally got wifi working on my t440s with Ubuntu 13.04.
I did exactly what is recommended in this answer, except that I obtained Wireless 7260 from here instead of the link in the answer that seems someone's dropbox.
Although from this website the wifi module bundled on t440s is Intel Wireless-N 7260, the driver I mentioned above, Intel Wireless 7260 (without N), seems to be working fine.

Update) After some while, wifi has been working fine with a following few tweaks:

Every time I reboot the OS, I need to run:
sudo modprobe iwlwifi
Though only occasionally, wifi device goes inactive and I have to build iwlwifi again same as in the link above:

cd Desktop/backports-3.11-rc3-1/
make defconfig-iwlwifi
make
sudo make install

Comment: **Note for all new owners of the T440s:** The current LTS-Version Ubuntu 14.04 detects all hardware components automatically, including WiFi and WWAN. =)
Answer: As my answer in wireless not enabled in 12.04, Lenovo T440s points out, if you want to get wireless in 12.04 you have to use a newer kernel version, 3.11 or later. I also have Intel Wireless 7260 (not sure about the -N) and either of the following works:

Installing a newer kernel version, by installing linux-generic-lts-saucy, in 12.04.3 or older.
Using the recently released and latest version, 12.04.4, from http://www.ubuntu.com/download/desktop

Note that Ubuntu 13.10 also works out of the box (but I personally prefer LTS versions).
Answer: I had the same problems in the beginning, trying 12.04 and 13.04
It was solved after upgrading to 13.10 where all drivers where detected automatically.

To enable wireless on Thinkpad t440s


Clarification on sharing a neutral for 2 circuits

Question: This is a followup question to this answer
I plan to do something quite similar to the diagram Tester101 posted. I will pull 3-wire (10/3+g) into a junction box and then pull 2 separate 2 wires (12/2+g) from the J box to GFCI outlets. Here are the things I am looking for clarification on...
--Can I run two 20amp circuits with the above configuration?
--If so, will the dual pole breaker be labeled "40 amp" back at the panel? (like a large appliance 240v breaker, or is there some other kind of dual pole breaker that I should look for?)
Thanks!
Answer: You'll use a double pole 20 ampere breaker. It will have two handles that are tied together, and will say "20" on the handles.
It should look similar to this...

(source: homedepot.com)
I'm not sure why you're planning to run 10 AWG conductors, but typically 20 ampere circuits only require 12 AWG conductors.
Comment: That's helpful, thanks. One addendum to this question: Does it matter if the two circuits that share a neutral do not both go to GFI outlets? I am thinking of using one circuit for an electric stove top and the other for a GFI outlet. Any problem with that configuration (in terms of GFI tripping or otherwise)?
Comment: No, both legs do not have to feed GFCI receptacles.  Not sure about the legitimacy of using one leg for a cooktop though.
Comment: Why might the cooktop be a problem? This is a 110v 2-burner cooktop rated to be used on a single 20amp circuit. Is it a code issue perhaps, that the cooktop is supposed to have its own "dedicated circuit"?
Answer: Yes, this is a perfectly valid configuration.
The breaker will be labeled as a "20 Amp" breaker. The breaker ratings are per-conductor and not total current.
They generally look just like the "large appliance 240v breaker", though there do exist some which put four breakers in one module called a "quad". One uses the quad breakers when there are no empty slots in the breaker box (though one must double-check that the box is certified to be able to be used with the "quad" breakers").
Comment: How would a quad breaker help if there were no empty slots?


Can AEM live copies have different domain in MSM?

Question: In AEM, say for example we have live copies for US and Russia and French countries in MSM and they all want to have different domain for every live copies
for example,
<code>For US - www.abc.com/en.html
For Russia - www.def.com/rus.html
For French - www.ghi.com/fr.html
</code>
We may need to create separate vhost and dispatcher and sling mapping settings to achieve above but just want to know whether it is recommended in MSM and want to hear what are the issue would come if we have different domain for each live copies. Please share your experience if you have faced before.
Answer: This is a common occurrence. Have done this with live copies before and works just fine. Getting the sling maps right can be tricky for the uninitiated but nothing extraordinary. You need not have to have the country prefix in the path if the domains are unique, not sure of the intent behind having them in your case.


Does the electricity meter move at a different speed depending on how many appliances are being used?

Question: I was wondering if it could be used as an economy measure to wash, vacuum, etc. at the same time rather than at different times of the day.
Comment: You are charged by the kilowatt hour used if you use a kilowatt in one day or 19 days the cost is the same. Some power suppliers offer lower per kw rate during off peak times. That is why a timer on a big power user like a water heater can be helpful.
Answer: The meter's speed is determined by current flow, so it moves faster or slower depending on electrical consumption.
What may change at different times of the day is the price per unit of electricity. So don't worry about meter speed, check with your utility supplier to determine what time of day would be most economical to consume the most.
Comment: As a UK poster, the Economy7 tariff (cheap rate from 11pm to 7-ish am) wouldn't work for me - I have gas water and central heating and gas cooker.  My current tariff is 1 kWh at 16.19p.  Power shower, telly, lights and laptop are the only normal daily use electrical items, the only other big thing is the washing machine.
Comment: I had the idea that as I vacuumed then showered while the washer was on, it might save a bit.
Comment: Probably not, unless you do it during a time period when *rates* (money/unit cost) are reduced. Conversely, if you do it during peak usage hours (a time when *rates* are often inflated) you may very well pay more in the end.
Answer: There are three factors in an electricity bill (beyond taxes and miscellaneous fees):

Usage = price per kilowatt hour (kWh)
Demand = price per kilowatt demand (kW) = a price based on the time when you are using the most kilowatts - i.e., the most electricity at one specific time. Typically measured over 15-minute intervals and billed based on the highest 15-minute or 30-minute (consecutive intervals averaged).
Time-of-day - The Usage rates may vary depending on the time of day (weekday daytime highest, morning/evening lower, nights/weekends lowest) and the Demand charge may be based on the peak demand of the highest usage category (i.e., only of weekday daytime - if your peak demand is at night then it doesn't count towards your bill)

Modern smart meters can easily track all this information and, typically, will report back to the utility the usage & demand for each 15-minute interval - i.e., 192 values per day (96 demand, 96 usage). They may also report lots of other information (voltage, current, frequency, power factor, etc.) but most of the other information has no effect on your bill.
In most areas (at least in the USA), demand charges and time-of-day usage charges are only applied to commercial accounts and not to residential accounts. My take on that is that there is a perception by users that charges for demand or for time-of-day usage are "unfair". They actually can represent real charges to the utility - and in that sense are quite "fair" to the customers in representing actual costs to the utility, though there is always some question as to how much of the costs, particularly with respect to demand charges, are "real". A utility generally pays the most for the highest demand period when the always-on (hydro, nuclear, etc.) power sources are not enough and additional generators are needed (e.g., natural gas). There are also infrastructure charges which are related to the highest possible demand.
In many areas, in addition to time-of-use not even being reported to residential users (let alone charged at different rates), usage charges are often at the same rate for on-peak vs. off-peak for commercial users.
End result: Check your utility bill. If you have different rates for on-peak/off-peak OR if you have a Demand Charge (it will show a calculation based on kW, not kWh) then you will typically benefit from running high-usage appliances at night to get the lower off-peak rate and/or to keep the peak demand as low as possible. If you don't have either of these then it makes no difference when you use your appliances.
Comment: Good answer I had the feeling OP was thinking of electric usage like gas mileage in a car. 100 mph gets you far fewer miles per gallon. but multitasking with home appliances is not more expensive than doing a task at a time right?
Answer: It depends, usually on old analogue meters with only a single row of numbers, time of day isn't taken into account, so use all the energy you need when you need and the price will be the same Ex: you can use a 3kW appliance for 20 minutes or a 1 kW appliance for one hour and you'll be charged for a unit.   Business connections are also charged for reactive power (a phantom power that has to be delivered but that is not turned into work). Other fees here includes: dispatching, power-rate (we are charged differently for different power available, most common rate used for home is 3kW), Other fees: nuclear decommissioning, renewable power incentives, poor people help, railway, TV tax, excises and VAT.
Here (Italy), we're on electronic meters and we may have a 'mono-tier' tariff so we pay the same rate all day round, a bi-tier tariff and we have different rates (actually very similar) for on-peak tier F1 (8:00-19:00 mon-fri) and off-peak tier F23 (19:00-8:00 mo,n-fri, h24 sat+sun) and a tri-tier tariff similar to 2-hour but with F2 tier counted on it's own F2 (07:00-08:00, 19:00-23-00 mon-fri , 07:00-23:00 sat). First two are common for home connections, the latter is for commercial.
Answer: An old spinning-disc meter won't do any of the fancy rate tricks like peak surge, time-of-day, discounts etc.  So we can exclude those.
The meter records accurately the energy you actually use.  There's no free lunch. There isn't a way to "game the meter" to get power cheaper by running any combination of appliances in particular ways.
Now if you ever get on a different rate plan (necessitating a smart meter) then things like time-of-day, peak energy flow, things like that will start to matter.


How can I stop my server being an "open DNS resolver"?

Question: This morning I got a letter from my ISP saying that my Ubuntu Server (13.10) can be used as an "open DNS resolver". They propose 3 possible solutions to fix this problem:

Limit access to recursive servers
Authorisation servers are not
allowed to implement recursion
The access to your server could be
limited by using a firewall

So I did some research and found out (through http://www.openresolver.jp/en/) that my server is indeed vulnerable. The problem however is, that according to my whereabouts, I never installed an DNS server on the machine.
After reading a bit more, I found out that DNSMasq is installed by default and people suggest securing it, but I can't find any config file whatsoever. When I run ps aux | grep dnsmasq, I do get:
server   21966  0.0  0.0   8172   952 pts/1    S+   09:09   0:00 grep --color=auto dnsmasq

Also, I have a feeling my OpenVPN service could have something to do with it. My server is still vulnerable even after I disable my OpenVPN service.
I would be thankful to anyone who has any tip(s) or advice on how to make my server safe and shut down or secure the responsible service. If I don't manage to solve the issue, my ISP will disconnect me from the network.
Comment: Can you stop the OpenVPN service and rerun the openresolver check?
Comment: @Jos, Just stopped the OpenVPN service and the server is still vulnerable.
Answer: If you don't have any service for other user from internet on your server simple sudo ufw enable. Status of fw you can see with sudo ufw status. This will turn you firewall app with default deny all policy for inbound traffic. This will also prevent "Open DNS resolver" to talk with internet without control
Comment: Thanks for your answer. I just turned on the firewall using ``sudo ufw enable`` but http://openresolver.com/ indicates that the server is still vulnerable.
Comment: Something is wrong. How openresolver.com can test your ip if ufw is enabled and do not accept connection? Test from site not from terminal because when you test from terminal you initate connection and will pass through fw. Also when you do testing you can watch log of ufw with `tail -f /var/log/ufw.log`. If connection is blocked you will see.
Comment: I did test it from the website by filling out the IP-address in the form on the bottom of the page. I checked the logs and nothing comes up. Also, I noticed that after enabling the firewall, my web server went down too on that domain. Could have anything to do with the router perhaps? Thanks again.
Comment: If topology is `server [private ip]` -> `router lan port [private ip]` -> `nat on router` -> `wan router port [public ip]` -> `internet` you are scan public ip, router ip, not server. Probably on router you have port 80 fw for web server. In this scenario stop ufw with `sudo ufw disable` and you web servis will be up. If you can give me more detail about topology of you network and service if you wish to solve problem.
Comment: It's a pretty straightforward setup. There's a router to which the server is connected through LAN with a static IP. The server has the following network interfaces, configured for OpenVPN I believe: ``auto lo
iface lo inet loopback

auto br0
iface br0 inet dhcp
  bridge_ports em1``
Comment: Ok. Your router do NAT for local network. Look port fw rules. Bring up firewall on router and allow only needed port's.
Comment: The problem was the "Enable DNS Relay" option in my router (see my answer). Thanks a lot for helping out!
Answer: I was able to fix the problem by unchecking an option that was turned on by default on my router. After I disabled the "Enable DNS Relay" option, I passed the test at http://openresolver.com/.


Show these affirmations using the context-free pumping lemma properties

Question: The question :
Given $G$ a context-free grammar of Chomsky normal form with $k$ symbols. We know that the language $L(G)$ satisfy the pumping lemma for $ p = 2^k + 1$
I have those two questions to proove:
1. Show that for each word $w$ in $L(G)$ such as $|w| \geq p$, a word $w'$ exist in $L(G)$ such as $|w'|\lt|w|$
EDIT THIS ONE IS ANSWERED ( SEE COMMENTS, 2 AND 3 LEFT)
In other words, if a  word w exist in the language generated by the grammar that has a superior length than the pumping length then an other word w' exist and has a smaller length than w but is in the language.
2. Show that if $L(G) \neq \emptyset$ then a word x exist where $x$ is in $L(G)$ such as $|x| \leq p$
3. Show that this language is decidable $NV_{GNC}$ = {< G >: where G is a context-free grammar in Chomsky normal form and $L(G) \neq \emptyset $ }
This is my proof for 3 ( feel free to correct me if this doesn't work.)
If we have a grammar in Chomsky normal form, H, then it takes H exactly 2n − 1 steps
to generate an input of length n. (Unless n = 0, in which case it requires 1 step.) Thus, if G is
in Chomsky-normal form, we can design a turing machine that will generate all derivations in G
of length 2|w| − 1. Since there are finitely many rules, this can be done in finite time and space.
At that point, we only need to check all of our derivations against w; if any one matches, we
accept; if none do, we reject.
Note : to answer 2 you can use 1 and to answer 3 you can use 1,2.
I am really not sure what step i need to do to proove that, if anyone could help it would be appreciated.
Answer: HINT for 1: Apply the pumping lemma to $w$ and pump down; i.e., in this version of the pumping lemma let $n=0$.
HINT for 2: If not, let $w$ be a word in $L(G)$ of minimal length, and apply 1 to get a contradiction.
Comment: @codetime: Yes, that's the argument for **1**.
Comment: @codetime: You need **2** for **3**: **2** tells you that if $L(G)\ne\varnothing$, then $L(G)$ has a word of length $\le p$. Thus, you need only generate all possible derivations of length at most $2p-1$, which you can do in finite time, and see whether any of them produces a terminal word.
Comment: @codetime: Look at what you're trying to prove in **1**: you're trying to prove that something is true for **every** word of $L(G)$ with at least $p$ letters, so of course you can't be given a particular $w$. But you know that of $|w|\ge p$, then $w$ can be decomposed into five segments, $w=uvxyz$, so that $|vy|\ge 1$, $|vxy|\le p$, and, most important for this problem, $uv^nxy^nz\in L(G)$ for **every** $n\ge 0$. What happens if you set $n=0$?
Comment: If n = 0 then i am left with $uxz$  and since $|uxy| \geq p$ we know that $|uxz|$ is smaller than p since  theres no more $y$ and so it prooves to us that $|w'|$ smaller than exists ? Is this right thanks
Comment: @codetime: No, $|vxy|\color{red}{\le}p$. We don't know that $|uxz|p$. Now apply **1** to get a contradiction.
Comment: ohhhh it's a contradiction proof now it makes sens. And we know that such a word exists because from 1 theres always a word smaller than w, neccesarily we have a w a the top, not sure if i see it from the good eye, but there's a maximum |w| that we reach. Could you give me an insight on 3. ( i tried an answer) i think we don't need 1 and 2 for this one. Thanks you so much again.


Securing Cabinet to wall: better to use two anchors to drywall or one screw into stud?

Question: I am aiming to babyproof this unit to the wall. It is three separate, vertical pieces. For the rightmost and leftmost piece, for where they are, there is one stud behind each of them.
I can secure them to the wall by either using the default drywall anchors they came with or I also have these and can secure them to either the drywall (two on each corner) or the stud in the center.
Which is more secure / recommended?
Is there a better, more safe way to go about this?
Thank you
Comment: Two screws into stud...
Answer: I would not rely on either drywall anchors or the Amazon thingies. I would attempt to find the wall studs behind the units, then drive decent wood screws through the unit into the studs.
If the back of the unit is thin masonite or paperboard, I have used L brackets driven in to the top of the cabinet, aligned with the studs, then driven screws through the brackets to the studs:
Comment: "A decent wood screw" would be something like a [cabinet screw](https://www.grkfasteners.com/products/cabinet/cabinet/) which is designed to support a _lot_ of weight (think how heavy your dishes are) and have a built in washer head to help ensure that the cabinet doesn't pull off the screw. _Note: not a recommendation of GRK, just a fan & the first example that came to mind._
Comment: You could use a longer screw that accounts for the gap (Consider an inch of penetration into the stud, 1/2" nominal through the drywall, 3/4" space behind the cabinet, thickness of the cabinet back and L bracket if needed: add all that up for the screw length you'd need. Use cabinet Screws with flat washer-like wide heads, like @FreeMan recommends.)  You could also use a 3/4" piece of wood as a spacer at the top. If you can pre-drill holes in the wood at the correct locations you can drive the screws through the wood, or lay a strip of wood on top of the screw shafts while tightening,
Comment: 100% recommend putting some 3/4" plywood or "1x" dimensional lumber (actually 3/4" thick) behind the cabinet to fill in the space @MarcJ. Otherwise, as you're tightening the screw, you'll be bending the back of the cabinet out of shape and it will fail long before its time. You won't need to cover the entire back of the cabinet, just a couple of strips, maybe 3-4" wide, through which you'll run your screws. One strip at the top, one at the bottom.
Comment: One thing I forgot to mention -- because of the molding on the floor, there is about a 3/4" gap between the back of the unit and the wall. What is the best way to secure this to the wall given the gap?
Comment: The trick here will be finding a screw that will hole through the "ceiling" (which is likely to be particleboard) without the pointy end sticking up.  Maybe OP DGAF.  Or might end up needing to use an all-the-way-through machine screw and nut.
Comment: To add my 2 cents, "the Amazon thingies" are just for preventing tall furniture from immediately tipping over, not for holding any amount of weight for any real length of time, so they should definitely not be used for anything even remotely structural or load bearing. These straps likely won't even prevent someone from trying to rip a dresser from it's place.
Answer: Screw into studs, and screw the center section to the side pieces.
Comment: *one screw into stud?* Naw, man. It ain't that kinda party. 15"... sure; this is half way to 15'. So like *four* studs. Using washers, 3" deck screws, and into the one-by that makes up the top rail on the inside. Then some screws into the top rail, through the top. No top rail? Then make a top rail, x3.
Answer: Studs are always recommended over drywall anchors.
Drywall anchors just might hold for a baby, but wait three or four years and they will not hold.
Answer: Although your question asks for something 'more safe', the engineer in me wants to point out that the risk such anchors mitigate is from someone climbing up the unit, so is a moment acting around the fulcrum of the corner at the bottom, and as such the unit acts as a lever reducing the force acting on the anchor by the ratio of its height to the distance of baby's centre of gravity from the unit, about ten to one.

So pretty any type of anchor into the plasterboard will work for preventing the unit toppling if an infant tries to climb it, the cheap ones which come with flatpack furniture will suffice (I don't worry about it if I have to climb up some shelves so secured), but use a better one if that means you don't worry about it. A good screw into studding will hold for 50kgf/100lbf, which means a whole football team could climb on it without it toppling over.
Answer: I find steel-cable + brackets Something like this (Amazon) the most flexible option for anti-tip protection.
Pros:

The bracket can be fixed to the wall and the furniture, even if the furniture doesn't sit flush to the wall
Steel cable is far stronger and far less likely to succumb to fatigue before your children are grown than plastic straps.
If you have a basic understanding of the angles involved, this system frees you from requiring a direct perpendicular anchor point. For instance, if your furniture sits between two studs, you can brace into the studs and diagonally out from the furniture and still have a quite robust hold .
If you're moderately handy, you can buy a spool of wire and the brackets individually and make these yourself to whatever length you require. (Honestly, you probably don't even really need the fancy brackets and cotter pins. Simple cable clamps to put loops in the wire, and a protruding screw head or a sturdy hook bolt as anchor points is probably fine, but use your best judgment)
They're reusable, and can be temporarily disconnected
Answer: It occurred to me you could place something like carpeting foot protectors under the legs far from the wall, but not under the legs next to the wall.  In this manner, the cabinet would always be leaning back into the wall.
This can be visualized with Pete Kirkham's drawing, with the little circle at the lower right imagined as being one of the foot protectors which raise the front of the cabinet, but not the rear next to the wall.  The larger arrow at the top then shows the force direction of lean.
I used this technique to stabilize bookshelves that sat on carpeting with thick padding, though in that case, instead of foot protectors, I used a strip of wood that was as long as the bookshelves were wide.


spring3 spel if-then-else with bean and place-holder in @value

Question: i have a problem with Spring SpEL to evaluate if-then-else construct in @Value annotation:

1. my config.xml file:
<context:annotation-config />

<context:property-placeholder location="file://${HOME}/maven.props/${USER}.properties" properties-ref="props" />

<bean id="props" class="java.util.Properties">
    <constructor-arg>
        <props>
            <prop key="interfaceName">createupdateproduct</prop>
            <prop key="destImportFilesDirectoryPath">${batch.job.import.zipDestinationPath}/${interfaceName}/imported</prop>
            <prop key="sourceImportFilesDirectoryPath">${batch.job.import.sourceImportPath}/${interfaceName}/import</prop>
            <prop key="reportDirectoryPath">${batch.job.import.reportPath}/createupdateproduct/report</prop>
        </props>
    </constructor-arg>
</bean>

2. in my bean, i have:
@Value("#{jobParameters['destFile']} ?: ${destImportFilesDirectoryPath}")
private String destImportFilesDirectoryPath;

Question. I would like to switch to the place-holder value, if the bean jobParameters['destFile'] is null. jobParameters is a bean that Spring Batch puts in the context.
The previous code-snippet does not work correctly but both #{jobParameters['destFile']} and ${destImportFilesDirectoryPath} are evaluated correctly singularly :O
I try different solutions, like:
@Value("#{jobParameters['destFile']} != null  ? #{jobParameters['destFile']} : ${destImportFilesDirectoryPath}")
OR
@Value("#{ org.apache.commons.lang.StringUtils.isNotEmpty(#{jobParameters['destFile']}) ? #{jobParameters['destFile']} : ${destImportFilesDirectoryPath} }")
OR
@Value("${ #{jobParameters['destFile']} ?: ${destImportFilesDirectoryPath} }")
but nothing works correctly !
Comment: Don't use a placeholder use SpEL instead. Something like `#{props['destImportFilesDirectoryPath']}` should do the trick.
Comment: can you explain me the complete expression ?
Answer: The
@Value("#{jobParameters['destFile'] ?: props['destImportFilesDirectoryPath']}")

solution does not work: if ("#{jobParameters['destFile']} is valorized, its value is assigned, otherwise it assign null value in the string property.
I have tried also following solution, but without success:
 @Value("file:#{jobParameters['destFile'] ?: T((java.util.Properties)props).getProperty('destImportFilesDirectoryPath')}")

To differentiate the batch jobs, i use other parameters, like user, interoperability interface name etc.
Comment: Please comment my answer, don't open a new one :)
Answer: If you want to use spEL try
@Value("#{jobParameters['destFile'] ?: props['destImportFilesDirectoryPath']}")

but, IMO, best option is to fill 'destFile' with the right value before start the job itself.
Also I have a (maybe stupid) question:
job parameters are used to identify in unique manner a job; are you sure you want to store 'null' as jobparameter value and change it during runtime? what happens if you run job the second time?
Comment: Your question is comprehensible question, but i have others parameters that make me able to
differentiate my restartable job, like user and the interoperability interface name. :) thank you to answer me, i try your solution
Comment: `@Value("#{'jobParameters[destFile]' ?: '${destImportFilesDirectoryPath:}'}")`
Comment: The

    @Value("#{jobParameters['destFile'] ?: props['destImportFilesDirectoryPath']}")

solution does not work: if ("#{jobParameters['destFile']} is valorized, its value is assigned, otherwise it assign null value in the string property.                                                      i'll try @Value("#{'jobParameters[destFile]' ?: '${destImportFilesDirectoryPath:}'}") :)
Comment: @Value("#{'jobParameters[destFile]' ?: '${destImportFilesDirectoryPath:}'}")                                not work again, it assign: "[class path resource [jobParameters[destFile]]]" value.


What's the difference between the "multilingual" and "language" tags?

Question: There are two tags, multilingual and language. Multilingual seems easy to understand: It is for sites that have more than one language and are negotiating the complexities involved in that arrangement.
However, language has no tag wiki and looking at the questions it looks like most of them could be considered multilingual and in fact many of them already are. Is there a reason for a separate tag to exist?
Comment: There are then [tag:translation], and [tag:locale].
Answer: Since language is now declared to be a synonym of  multilingual (see screen dump below), I assume that this has been resolved and there isn't supposed to be a difference.  Fine!

However, this is only a start.  IMHO, the "language"-related tags in the tag wiki needs to be re-thought.  In addition to language and multilingual, there are translation, localization, internationalization, i18n and locale.
The last two are supposed to be about two specific modules.  The i18n tag is used correctly (most of the time). However, few of the questions tagged locale is about the (core) locale module.  Many users use this tag for questions about PHP setlocale() - or for general questions about localization and internationalization.
As for the other language-related tags, they seem to be applied more or less interchangeably.
The current tag description of localization, says that localization is:

usually related to translating content

I find this description unfortunate, because it differs from the industry's standard definition.  Localization is about catering for regional differences and regional technical requirements (e.g. currency, units for weights and measures, character set, timezone, and translation).  Maybe some bold editor can change the description into something that makes it less a synonym of "translation"?
Usually "localization" and "internationalization" are regarded synonyms.  Not so here, where internationalization (no description) seems to be used as yet another synonym for multilingual.
I suggest internationalization is made a synonym of localization, and that the tag description of localization is improved.
(Some may argue that these are not synononyms.  However, I do not think ordinary users know enough about this to be able to distinguish between these two closely related and overlapping terms.)
Then, keep i18n and locale - but try to enforce that they should only be used for questions about the respective modules.
That leaves translation.  Like  language I think it just should become a synonym of  multilingual.
Comment: Tags can't be removed as such; they can be blacklisted, but that only happens in extreme circumstances (more extreme than this situation). If a tag exists with no associated questions, it will be automatically removed as part of a daily script, but there's nothing to stop it coming back in a subsequent question (or indeed if it's added to an existing question). Re-tagging is usually the solution to this sort of issue if people feel strongly enough to carry out the work. Since this is about different tags than the OP it might be worth opening a new meta question to discuss it
Comment: Thank's for the insight into the inner workings of SE.  I've edited my answer to *not* have removal of existing tags an option.
Comment: @Clive, apparently, tags can be removed.  I recently did a foolhardy attempt to curate the [tag:Notice] tag (it was without an excerpt, but I checked all instances and its usage was consistent and simple to describe).  It took about 30 minutes before my edit was rejected and the tag was removed (by a diamond mod). To me, it looks like curated tags is depreciated at Drupal.SE.
Answer: There are questions about the language that are not about a multilingual site, such as How do I get the $language object inside a form builder? and Best practice for language key for "und" in hook_form_alter; vice versa, a question about a multilingual site is about the language.
The options I see are:

Remove language from questions that are not about multilingual sites (such as the two questions I reported as example), and merge language with multilingual, keeping it for questions about sites where at least a language other than the default one is enabled.
keep language, and make multilingual its synonym.

Keeping multilingual would make more evident the question is about a multilingual configuration.
Comment: I would suggest keep locale and mark all others as synonym.


Scene disappears when zooming in Unity 2D

Question: I would like to start off by saying that I have absolutely no idea how this happened, it started doing this when I started using Unity today and have literally no idea where to start debugging...
So, this below screenshot shows the maximum I can zoom in on computer 1:

If I try and go any further this happens:

Meanwhile, opening the same project on computer 2, I can zoom in even further than the demonstrated amount...

Any help would be greatly appreciated
Update:
It is NOT a duplicate question, clipping plane does not affect the scene window, it is a property of the Camera component and as I clearly show this problem occurs only in the scene tab, not the game tab.
Comment: Really really strange that @Programmer marked this as an exact duplicate of https://stackoverflow.com/questions/40011722/when-i-run-the-game-scene-on-unity-my-panels-dissappears
Comment: @juanitogan It's a disappearing issue which is caused by "clipping plane ". Can you explain why it's not a duplicate?
Comment: @Programmer, I shouldn't have to.  I would just be restating the content of both the questions and the solutions.  This one is about the scene camera in the editor and the other is about the game camera... which don't share the same solution.
Comment: @Programmer  Ingame camera and editor virtual camera are different. Ingame have Inspector, editor one doesn't even exist in runtime. You didn't read the question carefully.
Answer: Try double clicking on a canvas GameObject in a Hierarchy.
A zoom thing happens to me when I work with huge objects. Editor camera adjusts to an editable object. So if you are working with a terrain that is 1000x1000 units, a small house that is 5x5 units will be clipping in closeup, when you will zoom to it manually. Double clicking zooms correctly to an object and adjusts editor camera clip planes.
Hope it is your case. Tell me if it so.
Comment: How to unflag this question? It is not duplicate. This solution won't fit there. Because it is an editor camera bug, but there (link by Programmer) is a game camera thing.
Comment: Yeah, this worked! Thanks!
Comment: I was lucky today. To find your question. It's hard for me to get rep here. I always find answers to my questions in super old posts, so I don't need to ask. Or I figure things while writing the question. Or I just am too late to answer something I can :(
So it was a good birthday present from you. :)
Comment: You can also get rep up by asking questions ;)
Comment: That's the thing. You can look at my questions. Even when I ask something it is when I'm confused, that I can't even formulate it. And I usually want to do something complicated, so nobody will even try answering that or I'll get myself how to do things.
Comment: Thank you!  I've been wondering what changes the zoom limits for a very long time.  My work process never involves intentionally double clicking objects in the hierarchy but I do occasionally accidentally double click them.  Thus, the zoom limits seemed to be changing at random on rare occasion.  (And, yeah, not a duplicate of that other question.)
Comment: Yeagh. That was annoying me until I figured this out.
Comment: Still relevant in Unity 2019.4.10f1
Comment: And it is still marked as duplicate


How to update a time-series model (such as facebook_prophet) on new data without retraining the data in full each time?

Question: Is it doable to update my trained model (fbprophet) in order to surpass the dilemma of retraining the whole dataset every time?
Thanks for your help in Advance
Answer: I found a solution using the warm-start approach, here.


Converting and size reduction of .m2t video for Flickr?

Question: I have a .m2t video file that I would like to upload to Flickr.
Can someone advise the best way to go about reducing the file size to <1gb and changing the file type to one that is accepted by Flickr?
Answer: For conversion and compression you can use HandBrake.
<code>sudo apt-get install HandBrake
</code>
Or download it from the website.
To reduce the size use a good compression algorithm like H.264.


Does the value of destroyed property have a gameplay effect?

Question: The value of destroyed property is briefly shown on the upper-left corner of the screen, for example 500$ after tearing a sofa. Is there any reason to avoid destruction, do I for example have to reimburse out of my own pocket?
Comment: IIRC, the only purpose is for getting the achievements that surround the damage dollar amounts
Comment: @Dpeif So no harmful effects? The PC version doesn't have achievements, looks like that causing damage has no impact whatsoever.
Comment: yeah, every [post](http://www.gamefaqs.com/boards/944011-ghostbusters-the-video-game/50019304) i find says its just for the console achievements, and nothing else. kind of a shame.
Answer: Sadly, no.
However, there are achievements / trophies for console versions associated with property damage:

Nice Shootin', Tex! - Complete the game with over $100,000 in property damage
The Destructor - Complete the game with over $3,000,000 in property damage


Why does Camel call my endpoint's start() method twice?

Question: I was using the start() method to instantiate the connection to the feed I was using and then stop() to tear it down, but after creating the Endpoint, Camel proceeds to call start() twice in a row on it
while obviously easy to work around by either checking whether its been started once already, or (as I did) just moving the starting code into the constructor of Endpoint - it's making me think I don't understand what the start() method is trying to achieve (the documentation helpfully suggests: start() starts a service ... grrreat
why would this happen twice?

incidentally createEndpointUri() gets called too... which makes me rather suspicious since this is a 'lazy loader for when a uri hasn't been supplied' ... except that one has been supplied ... so I wonder if some secondary instantiation is occuring for some reason
Answer: You should extend ServiceSupport which has doStart | doStop methods, and can keep track of the state of your class. Then Camel knows the state and wont invoke doStart twice.
Comment: Good idea, I have updated the javadoc of the class with more details: https://github.com/apache/camel/commit/4f7255af05a895ee932e5506605e55ceefd35de5
Comment: ah okay, I should have Overriden doStart() not start(). It works now thanks very much for answer. It would be cool if start() was final to stop that kind of mistake happening. I don't suppose you have any idea why createEndpointUri() is getting called? It ought to have it's URI already and it isn't a required override, but the code fails without it... maybe this is a separate question to be honest
Comment: You should put this somewhere in the documents


Do I have to get an online account to play the-crew?

Question: I just bought and downloaded The Crew game online to my ps4, and I am wondering why the button of creating a new game is not activated. Do I have to consider buying ps4 online voucher to play the game? or is playable offline?
Comment: The Crew is an [always-online game](http://www.playstationlifestyle.net/2013/06/21/ubisoft-confirms-that-the-crew-and-the-division-are-always-online/). I think you'll need online to begin playing it. I'm not sure how it quite works for PS4.
Comment: I found some more information - looks like you just need an internet connection and no subscription is necessary.
Comment: @TimmyJim Oh! do I have only to be connected to the internet or do I have to sign up for a monthly subscription account?
Answer: The Crew is an always-online game.
According to Ubisoft (the game makers):

Players with Xbox Live Silver or PlayStation Network accounts can play The Crew, enjoy Free Ride across the US map, play every mission and skills challenge, cross other players' paths, and participate in all the asynchronous activities that The Crew offers.
However, in order to communicate with others and to dive further into the multiplayer experience with Co-op and PvP, either a Xbox Live Gold or PS+ membership will be required.

It looks like you should be able to play so long as you're connected to the internet on your console.  Playstation Plus should not be needed.


How to update column name with respect to another column data in spark

Question: i have a requirement as below
df
+-------------+----+------+--------+----+
|          des|  id|veh_cd|veh_p_cd|code|
+-------------+----+------+--------+----+
|BodilyInjured|1001|    45|      50| BI_|
|BodilyInjured|1001|    12|      55| BI_|
|    Collision|1001|    18|      21|col_|
|    Collision|1002|    21|      25|col_|
+-------------+----+------+--------+----+

if the "des" is "BodilyInjured" then two separate column should be added "BI_veh_cd" for "veh_cd" and BI_veh_p_cd for "veh_p_cd" which should contain max values of veh_cd and veh_p_cd and the same goes for "Collision"
Note: Need to append respective "code" to "veh_cd" and "veh_p_cd" for column names and respective data should be populated. column name format should be code+columnname. ex: for BodilyInjured code is BI now column name should be "BI_veh_cd" and "BI_veh_p_cd"
expected output
id  BI_veh_cd   BI_veh_p_cd     col_veh_cd  col_veh_p_cd
1001    45        55            18            21
1002    NULL      NULL         21             25

Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162324/discussion-between-luzo-and-mtoto).
Answer: You are looking for the pivot functionality:
from pyspark.sql.functions import max

df1 = df.groupBy("id") \
       .pivot("code") \
       .agg(max("veh_cd"), max("veh_p_cd")) \
       .orderBy("id")

To rename your columns we can use vanilla Python regex:
import re

newnames = [re.sub("max|\(|\)", '', i) for i in df1.columns]
df1.toDF(*newnames).show()
+----+----------+------------+-----------+-------------+
|  id|BI__veh_cd|BI__veh_p_cd|col__veh_cd|col__veh_p_cd|
+----+----------+------------+-----------+-------------+
|1001|        45|          55|         18|           21|
|1002|      null|        null|         21|           25|
+----+----------+------------+-----------+-------------+


Find Area Under a Graph

Question: Let $f$ be a function defined (piecewise) on the interval $[1, 2015)$ by the formula:
$$f(x)= \frac{2}{k(k + 2)} , \forall x \in [k,k+1), \text{where } k=1,...,2014.$$
Find the area of the region bounded by the graph of $f$, the x-axis and the lines $x = 1$ and $x = 2015$.
Comment: hint: 2/(k(k+2))=1/k-1/(k+2)
Comment: Thanks for the reply, Mahbub. Could you give me a little bit more help? I don't understand how to even get started on this problem.
Comment: whats the area of the region bounded by the graph of f, the x-axis and the lines x = k and x = k+1 ?
Comment: OK, I have now found the area of the graph bounded by k and k+1 to be ln((k(k+3))/((k+2)(k+1)). Is this as far as i can go?
Comment: No its not, 'ln' will not come here, area will be 2/(k(k+2)) .1=2/(k(k+2)). Now sum k=1 to 2014 using my first comment
Answer: $$f(x)= \frac{2}{k(k + 2)} , \forall x \in [k,k+1), \text{where } k=1,...,2014.$$
$$\int_1^{2015} f(x) dx = \sum_{k=1}^{2014} \int_k^{k+1} \frac{2}{k(k + 2)} dx = \sum_{k=1}^{2014} \frac{2}{k(k + 2)} \int_k^{k+1}  dx = \sum_{k=1}^{2014} \frac{2}{k(k + 2)} $$
$$= \sum_{k=1}^{2014} \frac{1}{k} - \frac{1}{k + 2}= \sum_{k=1}^{2014} \frac{1}{k} - \sum_{k=1}^{2014} \frac{1}{k + 2}   $$
Write out what you get for those terms.
$$\{\frac{1}{1} + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{2014}\} - \{\frac{1}{3} + \frac{1}{4} + \frac{1}{5} + \cdots + \frac{1}{2016}\}$$
Do you see what happens here?
Comment: Thank you, this makes a lot of sense.

Find Area Under a Graph


Login screen is disfigured

Question: This is the second time I see it this way when I startup my machine. You can see that the screen is not fully utilized. Ubuntu's screen starts from the top-left, but doesn't fill the whole screen. The underutilized area isn't totally black. But it's a mauve background with white dots all over it, I think it's the stock background for the login screen but I'm not sure. This doesn't happen when I lock my screen.

What is causing this and how can I fix it ?
Answer: It seems maybe someone activated some zoom option of the "Universal Access Preferences" available in the lower left part or lower right of the GDM login screen that transforms the left part of the screen in a magnifier lens.
You can try setting it up such that it doesn't start on login using:
sudo -u gdm gconftool-2 /desktop/gnome/applications/at/screen_magnifier_enabled --type bool --set false

or purging that package (not recommended) using:
sudo aptitude purge gnome-mag

Source: http://ubuntuforums.org/showthread.php?t=1545815
Comment: Sorry, but GDM has been replaced by LightDM long ago and the `gnome-mag` package doesn't seem to exist on 14.04 either.

Login screen is disfigured


What is the difference between ObjectProperty and Binder in Vaadin?

Question: I'm going to read me in binder. Now I have three options in vaadin to binding Data. Once is with the ObjectProperty and setPropertyDataSource(). The Second one is to make a bean class and set it in a BeanFieldGroup and the third one is with a Binder.
What is the difference of it?
 Which one uses for what ?
Answer: ObjectProperty, BeanFieldGroup and in general FieldGroup is concept of the Vaadin 7 for binding data between the bean and the fields.
Binder on the other hand is new concept in Vaadin 8 (and also in platform versions 10 onwards) that replaces the above. So if you are going to develop the application with Vaadin 8, the Binder is the way to go.


generate sequence (and starting over in case of a recurrence) and add new column with highest number per sequence, within group, in R

Question: I am looking for a way to generate a sequence for a column with names of cities grouped by an ID. What is crucial is that when a name of a city is repeated (within the group) a new sequence has to start. A new sequence should also start in case of a new ID.
EDIT:
The question how to create the above mentioned sequence has been solved. To help select the row with the highest sequence number later on, I am looking for a way to add a new column to the data frame that shows for each record, per sequence, per ID the highest number of each sequence.
Here is an example of what I want to achieve, based on a simplified version of my data frame:
ID  City    Sequence    Highest_number
1   Nijmegen    1    2
1   Nijmegen    2    2
1   Arnhem      1    2
1   Arnhem      2    2
1   Nijmegen    1    1
1   Arnhem      1    3
1   Arnhem      2    3
1   Arnhem      3    3
1   Nijmegen    1    1
2   Nijmegen    1    1
2   Utrecht     1    1
2   Amsterdam   1    2
2   Amsterdam   2    2
2   Utrecht     1    4
2   Utrecht     2    4
2   Utrecht     3    4
2   Utrecht     4    4

mydf <- data.frame(ID = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2),
        City = c("Nijmegen", "Nijmegen", "Arnhem", "Arnhem", "Nijmegen",
        "Arnhem", "Arnhem","Arnhem", "Nijmegen", "Nijmegen", "Utrecht",
       "Amsterdam", "Amsterdam", "Utrecht", "Utrecht", "Utrecht", "Utrecht"))

Answer: Construct a 'run-length encoding' and use that to generate the sequences
rle <- rle(as.character(mydf$City))
mydf$Sequence <- unlist(lapply(rle$length, seq_len))

For the updated question, where two columns form the key, paste the columns together with a unique symbol and compute with that
rle <- rle(paste(mydf$ID, mydf$City, sep = "\r"))
mydf$Sequence <- unlist(lapply(rle$length, seq_len))

This will be 'fast', especially compared to a for loop.
Comment: Quick thanks for drawing attention to `rle()`! Very handy function I did not know about until now.
Comment: Thanks for the quick reply. Unfortunately, this solution does not cover the issue that the sequence also has to start over in case of a new ID (group by). I have edited my example so that ID 1 and 2 share a sequence of one city ("Nijmegen").
Comment: @Thijs Instead of creating an rle of `myddf$City`, create it from `paste(mydf$ID, mydf$City, sep="\r")`; this is the approach used in `duplicated.data.frame()`, for instance.
Answer: A good old for loop does the trick
mydf$Sequence <- NA

for(i in seq_len(nrow(mydf))) {
  if (i == 1 || (mydf$City[i] != mydf$City[i-1]) || (mydf$ID[i] != mydf$ID[i-1]))
    mydf$Sequence[i] <- 1
  else
    mydf$Sequence[i] <- mydf$Sequence[i-1] + 1

}

Comment: Thanks for the quick reply. Unfortunately, this solution does not cover the issue that the sequence also has to start over in case of a new ID (group by). I have edited my example so that ID 1 and 2 share a sequence of one city ("Nijmegen").
Comment: See my edit... just add another condition for resetting to 1, if I understand correctly
Comment: Thank you for your reply, it seems to work perfectly with the original data frame. I have edited my post and asked a new question (based on the question that has been solved). I have tried to solve this earlier on by using: mydf_2 <- mydf %>% group_by(ID, City) %>% mutate(Count_2 = tail(n()))  but that is not giving the result I am trying to achieve.
Comment: I'd personnaly go with `sqldf`, if you know SQL you should be able to do that without too much difficulty, using `create table max_values as select ID, city, max(value) from mydf group by id, City;`. Then merge to your existing data.
Comment: Thanks, I could not get create table to work (because of an error) but I managed to do this:   mydf_2<-sqldf("select ID, City, max(Sequence), from mydf, group by ID, City")   but this code does not take into account that for each sequence of cities, the highest number should be placed in a column because for each id only one unique city is placed in the data frame.
Comment: I see. I would say that this falls in the "ask a new question" area (we don't want questions to become too complex), so I would encourage you to accept whichever response is best for you and take it from there in a new post.


hourly to_period 24 hour format ignored

Question: Sample string:
11/8/2017 3:00:00 PM

Which is in the dataframe column sales['Checkin']
Code for conversion:
pd.to_datetime(sales['Checkin'], format = "%m/%d/%Y %H:%M:%S %p").dt.to_period('H')

This results in:
2017-11-08 03:00

Thus ignoring the PM... I expected correct result:
2017-11-08 15:00

I tested it and it seems to be an issue with to_datetime and not to_period, but I'm not sure what I'm missing because I googled and noticed I am using %H correctly, and not %I which is the 12 hour format...
Answer: You have to use %I rather than %H:
In [11]: pd.to_datetime(s, format = "%m/%d/%Y %I:%M:%S %p").dt.to_period('H')
Out[11]:
0   2017-11-08 15:00
dtype: object

From the datetime formatting docs:
%H  Hour (24-hour clock) as a zero-padded decimal number.
%I  Hour (12-hour clock) as a zero-padded decimal number.

Comment: @FredericBastiat ha, I see you mentioned it in your question :) I hadn't even see that, you were so close!
Comment: thank you for the follow up edit, also. I was struggling after changing *both* Hs to Is, which is incorrect

hourly to_period 24 hour format ignored


NSUTF8StringEncoding causes file to render improperly in TextEdit/Pages, but not BBEdit/vi

Question: I'm running into a bit of a weird issue.  Whenever I create a new text file in my iOS application, I set its encoding to be NSUTF8StringEncoding.  If I edit the file and input any characters with diacritics and save the changes, the diacritics render properly in some applications such as BBEdit, TextMate, cat and vi but not in others such as TextEdit, Quick Look and Pages.
I'm using the following code to save the contents of a UITextView to the plain txt files.
  NSError *error;
  NSString *dataString = self.textView.text;
  BOOL savedChanges = [dataString writeToFile:fullPath atomically:YES encoding:NSUTF8Encoding error:&error];
  if (!savedChanges)
  {
    // Pop up an alert saying something went wrong.
  }

The unix file command reports that the saved file is indeed "UTF-8 Unicode text, with no line terminators"
What's even weirder is if I save the file again without changing the contents of the text, the file will then render properly in Quick Look & TextEdit on my Mac.
Any help would be appreciated.
Comment: The `writeToFile:atomically:encoding:error:` method should automatically set the `com.apple.TextEncoding` attribute on the file correctly, which should be sufficient for Quicklook and TextEdit. I don't reproduce your issue. Perhaps whatever was causing your issue has been fixed since you ran into it in 2010.
Answer: This is a guess, but could it have something to do with a lack of a byte order mark? For UTF8, the BOM looks like EF BB BF in hex, and should be the very first thing in the file.
Comment: The BOM is not needed for UTF-8 (as its byte ordering is consistent regardless of endianness), and should generally be avoided as a lot of apps don't properly deal with a UTF-8 BOM.
Comment: However, after a quick test, putting the BOM in *does* cause QuickLook and TextEdit to interpret it correctly in this case. Possibly the file is too short for the OS to identify it with certainty as UTF-8. It's certainly treating it as MacRoman.
Answer: If you save a text file with an UTF BOM, and the com.apple.TextEncoding xattr is not set, any software that opens it will have to guess at the correct character encoding. Some apps guess UTF-8, some guess Mac OS Roman, and others guess something else.
You can replicate this behavior by saving a file as UTF-8 with no BOM, and then in Terminal give the xattr -d com.apple.TextEncoding filename.txt command.
To set the xattr, you would call setxattr(). There doesn't seem to be a documented way to set it via a Cocoa API. You could also prefix your data with the UTF-8 BOM.
There's the question of what character encoding should be assumed when the BOM and xattr are missing. Is it a bug if it defaults to Mac OS Roman? Should UTF-8 be the default?
Answer: Another alternative if you want to avoid the BOM is to set the com.apple.TextEncoding extended attribute to UTF-8;134217984 on the file -- See here for more details.
I don't know how you would do that from code, but xattr -w com.apple.TextEncoding 'UTF-8;134217984' filename.txt will do it at the command line to confirm that it fixes the issue for you.
Answer: I experienced the same issue just yesterday! My problem was that files created with by NSString writeToFile:atomically:encoding:error: were being read and interpreted by QuickLook and Text Edit perfectly, but files created by writing UTF-8 data straight to a file with NSFileHandle did not work in QL & TE (the automatic encoding got it wrong).
Peter Hosey pointed me to this question and after some testing it turns out that the NSString method does not use the BOM, instead it just uses the extended attribute com.apple.TextEncoding. With some code I found online I created an NSString category to allow you to set this value easily by passing a file path and a NSStringEncoding value.
NSString (FileTextEncodingAttribute) on gist.github
An example would be:
[NSString setTextEncodingAttribute:NSUTF8StringEncoding atPath:@"myfile.txt"];

I've tested this and it works perfectly :) Who knew writing a plain text file would take so much research!!


MongoDB how to get count of records by 30 hours ago, 48 hour ago, and one month ago?

Question: I have bellow structure:
{
   "_id" : ObjectId("58d8bcf01caf4ebddb842855"),
   "publishDate" : ISODate("2017-03-14T00:00:00.000Z")
},
{
   "_id" : ObjectId("58e87ed516b51f33ded59eb3"),
   "publishDate" : ISODate("2017-04-14T00:00:00.000Z")
},
{
   "_id" : ObjectId("58eb5b01c21fbad780bc74b6"),
   "publishDate" : ISODate("2017-04-09T00:00:00.000Z")
},
{
   "_id" : ObjectId("58eb5b01c21fbad780bc74b9"),
   "publishDate" : ISODate("2017-04-12T00:00:00.000Z")
}

Now i want to take count of records that are published before one month ago, count of records that are published before 48 hour ago, and count of records that are published 30 hour ago.
My current try is:
db.JobPosting.aggregate([
   {$project:{
      "thirtyDaysAgo":{"publishDate":{$lt:["$publishDate",new Date(ISODate().getTime() - 1000*60*60*24*30)]}},
      "fourtyEightHourAgo":{"publishDate":{$lt:["$publishDate",new Date(ISODate().getTime() - 1000*60*60*48)]}},
      "thirtyHourAgo":{"publishDate":{$lt:["$publishDate",new Date(ISODate().getTime() - 1000*60*60*30)]}}
  }},
  {$group:{
      _id:{thirtyDaysAgo:"$thirtyDaysAgo", fourtyEightHourAgo:"$fourtyEightHourAgo", thirtyHourAgo:"$thirtyHourAgo"},
      "count":{$sum:1}
  }}
])

But the result is wrong:
{ "_id" : { "thirtyDaysAgo" : { "publishDate" : false }, "fourtyEightHourAgo" : { "publishDate" : true }, "thirtyHourAgo" : { "publishDate" : true } }, "count" : 1 }
{ "_id" : { "thirtyDaysAgo" : { "publishDate" : false }, "fourtyEightHourAgo" : { "publishDate" : false }, "thirtyHourAgo" : { "publishDate" : false } }, "count" : 1 }
{ "_id" : { "thirtyDaysAgo" : { "publishDate" : true }, "fourtyEightHourAgo" : { "publishDate" : true }, "thirtyHourAgo" : { "publishDate" : true } }, "count" : 1 }

What i want is:
{moreThanThirtyDayAgo:{count:1}, moreThanFourtyEightHourAgo:{count:2}, moreThanThirtyHourAgo:{count:1}}
Answer: Use the $cond conditional expression to create a binary decision tree that will feed the evaluations to the $sum accumulator. Take for instance the following pipeline:
var dateThirtyHoursAgo = new Date();
dateThirtyHoursAgo.setHours(dateThirtyHoursAgo.getHours()-30);

var dateFourtyEightHoursAgo = new Date();
dateFourtyEightHoursAgo.setHours(dateFourtyEightHoursAgo.getHours()-48);

var dateMonthAgo = new Date();
dateMonthAgo.setMonth(dateMonthAgo.getMonth()-1);

var pipeline = [
    {
        "$group": {
            "_id": null,
            "thirtyHourAgo": {
                "$sum": {
                    "$cond": [
                        { "$gte": [ "$publishDate", dateThirtyHoursAgo ] },
                        1, 0
                    ]
                }
            },
            "fourtyEightHourAgo": {
                "$sum": {
                    "$cond": [
                        { "$gte": [ "$publishDate", dateFourtyEightHoursAgo ] },
                        1, 0
                    ]
                }
            },
            "thirtyDaysAgo": {
                "$sum": {
                    "$cond": [
                        { "$gte": [ "$publishDate", dateMonthAgo ] },
                        1, 0
                    ]
                }
            }
        }
    }
];

db.JobPosting.aggregate(pipeline);

Comment: One another question, how about adding two or three other condition like `ALL_OTHER_FIELDS like %IT% OR like %Software% OR like %xyz%`. `ALL_OTHER_FIELDS` is all fields that i have.


Migrating server configuration to new machine

Question: I have a VServer (Ubuntu 14.04) with some problems, where reinstalling the OS was suggested here and also by the hoster.
I was planing to clone all the configuration, installed packages etc. except the few faulty ones of course. I found a lot of instruction how to migrate different parts of the OS but no full how to/script. Since I'm very suprised about this fact I was wondering if somebody knows one.
It should include:

users
home
/etc
installed
packages
/var/www
/var/vmail
/opt
databases: mysql and postgresql
etc.

limitations are:

the servers do not have the same hardware
i can't install packages on the source system
only ssh access
Comment: @Rinzwind you are right but I'm afraid to miss out configurations or the problem is that I copied most of etc and have troubles now again. Setting up the services was a pain and I would prefer to not do it again especially because we should have no downtime.
Comment: What do you mean by "VServer"? is it a virtual machine in, say, VMWare? Or does that mean something else?
Comment: Don't. Really. Don't. "users" and "home" recreate them on the new server. Same for /etc, /opt packages and configs for packages. Do make a dump of your databases (mysqldump and pg_dump/pg_restore).  /var/www will be a tar file and an scp, /var/vmail is not a normal dir so that one is up to you.  Copying will also copy any problems into your new setup. Plus I would also assume you would start using 16.04 and not 14.04. Take the time to do the setup again, this time do a better job than the 1st time and document it.
Comment: "I found a lot of instruction how to migrate different parts of the OS but no full how to/script" That's because it is a bad idea ;-) Scripts tend to assume things and expect things to be set up a certain way. If you want a script it will be one for you and you alone.
Comment: @JDS yes the VServer is a virtual machine. I don't know which technique they use.
Answer: Out of my several years experiences with real hardware or cloud instances I would recommend you to not try to copy configuration from old installation unless it is something really specific.
Also now on 2016/12 I would not recommend you to stay on Ubuntu 14.04 unless you have something really hardly depending on this version.
I have "basic scripts" for different types of instances including all basic packages and settings I need - something like "Dockerfile" with all installation steps. It includes "must have" packages and it ensures you have stable environment when you create new machine.
Only case when I really clone existing instance is new mysql replica on GCE because it saves a lot of time and all replicas are supposed to be the same.
So as stupid as it sounds I would recommend you to "create machine from scratches". Because if you cannot solve problems on old machine it is very likely you could at the end duplicate at least some problems together with old configuration.
And one more thing - it is generally not exactly good idea to run mysql and postgresql on one machine or instance unless those are very small and not heavily used databases. Both databases have quite a different strategies for work with memory and disks and to get good performance from both databases you would need to fiddle a bit with their settings.
Comment: Thanks for pointing out the problem with the two database services, I did not know about that.
Answer: Cloning an entire server isn't usually the right approach. That just clones all the problems. The approach typically taken is as follows

Build new server
Install required software
Clone data

If you want to be able to manage the configuration of this and other servers, long term, at scale, and be able to quickly migrate the whole thing to a new system when the need arises, I highly recommend a modern configuration management tool that is designed to automate the deploy and build of a new server. Chef, Puppet, and Ansible come to mind. But I recommend Ansible because of it's ease of use and it's agentless operation.
There are some important details missing from your question, such as if this is a VM hosted somewhere, what software is installed, how the server is used. These details may require one to change one's approach to building a new system.
Answer: Clonezilla does the job, connect your new machine HDD via USB then bootup via clonezilla CD follow instructions too clone the source HDD to the external USB. be careful and make sure you cloning the right HDD to the right source. after completion of clone insert your HDD to the new machine.