Django template parsing order

Question: Is the parsing order for Django templates specified somewhere in the Django documentation?
Based on the documentation for writing custom template tags and the API, it seems Django uses a depth-first traversal for building the node tree.  However, I can't find any statement in the Django documentation that guarantees this.  In particular, I'd like to know if the node tree is completely assembled before any rendering is done.
Also, how does this affect template inheritance?  Are the {% block ... %} tags in the derived template parsed at their location in the base template or are they parsed by the {% extends "..." %} tag before the base template?
Answer: The answer is yes, all nodes are created when you create a Template object. But the magic happens when you call the render method of this object. This Template object has a NodeList with it that is rendered within a context. This rendering is done as you said before, depth first, and it just gets the inners child strings (html) appended to the father Node. Here is the NodeList class where the node rendering is done. So, the root node is the one to get all the nodes appended to it to finally generate the file. And if you remember, the extends template tag must be first in the template so it becomes the root node who get all the already rendered nodes in it.
Now, what about inheritance? How is the exteds root node rendered? Well, I haven't figured out all about the context role here, but there is a ExtendsNode that is created by the extends template tag. This type of node recieves the parent template name (your base template) and a context with all the variables, blocks and nodes of the child template (the one you actually call to render). So when this node is rendered, it does the same thing that every node does, append his child nodes. The main difference in this class is that it gets the nodes marked with the block template tag from the child's template context, and appends it everywhere you defined a block template tag with that name in the parent template.
So the nodes are created first, and the extends template tag manage to get them when you render the template.
If you want to know how nodes a created, you can look for the Token and Parser class at the Django code.
Hope this helps.
Comment: as I mentioned in my question, I know that's what's currently implemented.  However, I'd like some pointer to the documentation that allows me to conclude this is not just an implementation detail.  I prefer relying on mechanisms that are unlikely to change in future versions.
Comment: +1 for a good answer and making me realise that the django source is on github! I kept going to djangoproject.com look up the source

Django template parsing order


Fast build of a very large sparse matrix

Question: How to speed up construction of a very large sparse matrix where each row has only one nonzero element in correspondence of a column and each column has an equal number (on average) of nonzero elements?
I have a huge (sparse) matrix of size N1-by-N2, say for example of size 1e8-by-5e4, where each row contains only one nonzero element which is randomly chosen without replacement by numpy.random.choice(numpy.arange(N2),size=N2,replace=False).
As far as I know, the only way I can build the matrix is to run numpy.random.choice() in a for loop N1 times. As N1 is very large, to speed things up I am using scipy.weave:
import numpy as np
from scipy import weave
from scipy.weave import converters
import scipy.sparse as sparse # Cython import

def weave_sparse(N1,N2,w):
    conn_matrix = sparse.dok_matrix((N1,N2))
    fac = lambda N : np.random.choice(np.arange(N), size=N, replace=False)[0]
    code = """
           int i;
           py::tuple arg(1);
           arg[0] = N2;
           for(i=0;i<N1;i++) conn_matrix[i,(int) fac.call(arg)] = w;
           """
    weave.inline(code,['conn_matrix','N1','N2', 'w', 'fac'],
                 compiler='gcc',extra_compile_args=['-std=c++11 -Ofast'],force=0)
    return conn_matrix

Still, for N1 approaching 1e6 and beyond the code it taking too long to complete. I suspect that there could be a much more efficient way to build the sparse matrix. Any other strategy in mind to speed up and build the matrix in a human-readable time?
Comment: FYI: In the text of the question, you say `numpy.random.choice(numpy.arange(N2),size=N2,replace=False)`.  That is equivalent to `np.random.shuffle(np.arange(N2))` or `np.random.permutation(N2)`.  In the code you use `np.random.choice(np.arange(N), size=N, replace=True)[0]`.  That is equivalent to `np.random.randint(0, N)`.  (Why generate `size=N` and then take just the first element?)
Comment: @Warren Yep sorry. It should have been `False` in the code.
Answer: You shouldn't need weave to make this efficient.  Here's an example that should work for you.  I used small values of N1 and N2 to make it easy to inspect the result. I also used csr_matrix, but any of the scipy sparse matrix types should work with little or no changes.
In [50]: from scipy.sparse import csr_matrix

N1, N2 and the array w are basically the inputs; w is an array of length N1.  It holds the values that will be put in each row.  Here, I fill w with 1.
In [51]: N1 = 15

In [52]: N2 = 12

In [53]: w = np.empty(N1, dtype=int)

In [54]: w[:] = 1

Now create the csr_matrix:
In [55]: rows = np.arange(N1)

In [56]: cols = np.random.randint(0, N2, size=N1)

In [57]: conn_matrix = csr_matrix((w, (rows, cols)), shape=(N1, N2), dtype=int)

The .A attribute is just a shortcut for the .toarray() method; it returns a regular numpy array:
In [58]: conn_matrix.A
Out[58]:
array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
       [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]], dtype=int64)

Comment: Thanks @Warren. This is exactly the approach I was looking for. It put me on the right track.
Answer: So, the speed problem here can be recast as an efficient problem of building the very large sparse matrix. As @Warren pointed out np.random.choice(np.arange(N2),size=N2,replace=False) over all N1 elements is still a random permutation problem. So, after some thoughts, a concise implementation for the above could ultimately be the following:
N1 = 10000000 #1e8
N2 = 5000
rows = np.arange(N1)
cols = (np.floor(np.random.permutation(N1)/float(N1)*N2)).astype(int) # Randomly pick N1 objects and assign to N2 categories in almost equal proportion
w = np.ones(N1)
conn_matrix = sparse.csr_matrix((w, (rows, cols)), shape=(N1, N2), dtype=int)


How do I fix a sewer snake cable that has kinked inside the spooler cage?

Question: I had to snake out my sewer this weekend and rented a large sewer snake from the hardware store. When I hit the problem area the cutting tip on the end of the cable apparently got stuck and I didn't realize it in time. The snake model I rented has a foot switch and I kept my foot down, turning the spooler too long, trying to cut through the problem area. I have to admit I heard a couple of clanks inside the cage where the spooled cable is and that should have warned me, but being a newbie at this I thought it was just the sound of the cable vibrating from the work I was doing in the drain. Next thing I know I've got cable inside the cage all wrapped up around itself in several twisted loops. The cable wouldn't come out any further because it was blocking its own way.
My question is, how in the world do you get this unstuck? I went down to the hardware store, asked them. The know-it-all guy there said it would come unstuck, I just had to twist it back the opposite way. Well, I tried doing that, but that is spring steel cable we're talking about. The more you twist, it just wants to unwind all the harder. I stretched the exposed cable out straight, clamped some vice grips close up to where it exits the cage (which I blocked from turning with a screwdriver jammed in sideways) and tried twisting out the cable manually. That only worked to beat me up a bit when it got away from me and unwound my futile efforts. I tried running the machine in reverse and letting the vice grips brace themselves against a cinder block. The motor has enough torque to drive completely over the block, vice grips and me. I tried clamping one end of the cable in a bench vice, but this just made things worse by causing extra kinks in the cable outside the cage. All the while, my sewer is still backed up and we had a birthday party fastly approaching. I ended up calling in the real plumber and he got the drain flowing again. I still have to take the snake back in on Monday morning to avoid another $50 rental charge.
A related question would be, how do you get the kinks out of this type of cable, except I figured that one out by clamping the cable just below the middle of each kink in said bench vise and then wrestling the snake in the opposite direction of the bend. It straightened up pretty nice.
Sorry, my phone(camera) isn't working so I don't have pictures of this little "adventure" to share.
Comment: Sounds like the snake cable was defective. the idle end should not be prone to getting tangled.
Comment: @Jasen 10, yes, come to think of it, I remember seeing some previous kinks in the cable. I suppose a 20-year old rental unit might have seen some abuse in the past.
Answer: I found a solution in time to get the rental unit back to the store this morning. This whole ordeal was painful, but I think if I can share it in a way that helps someone down the road I'll feel better about it. So this is what worked for me: I took a crowbar and, after examining the loop and twists inside the cage more carefully, found a place to pry part of the loop out of a side opening in the spooler cage. It definitely wasn't easy, but from that point on it suddenly got really easy to untwist it and work the cable back in and then out the way it was supposed to exit.
Here's a similar machine like the one I used. From a websearch, I see some others out there that don't have the cage but a more or less solid spooler. My crowbar method probably wouldn't work on some of them.

When this type of cable kinks it creates an almost 90-degree bend in the inner core of the cable at the apex of the loop. This would not work very well as a drain cable anymore. So, when you finally get it untwisted the cable kinks themselves have to be straightened out. I used the method I mentioned in the question to straighten it out as I had the others. I clamped the cable in the bench vise just below the bend and then, using a lot of strength, forced the cable backward over the bend. I had to go beyond the straight mark so that it would spring back straight. I hope this answer helps anyone else that might find themselves in the same predicament.
Answer: When winding cable back in drum do not use rewind function. Use forward function or for best results push in manually without power
Comment: This doesn't really answer the OP's question.
Comment: This does not provide an answer to the question. Once you have sufficient [reputation](https://diy.stackexchange.com/help/whats-reputation) you will be able to [comment on any post](https://diy.stackexchange.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/143722)
Comment: Care to explain how running the motor _forward_ will get the cable back into the drum?
Answer: Running'forward'keeps proper tension and coil orientation, I have seen the coils permanently damaged.
This was intended to be a comment regarding the reason to always run the machine forward, I see that I am not qualified to 'comment' but thought the information was pertinent
Comment: This does not provide an answer to the question. Once you have sufficient [reputation](https://diy.stackexchange.com/help/whats-reputation) you will be able to [comment on any post](https://diy.stackexchange.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/172125)


Steam Web API not retrieving user reviews

Question: I have been trying to get some user reviews from an app on Steam using their API which is documented here. However, some of the requests return only the JSON {"success":2} and nothing else.
You can try these two requests on your browser:
Miner Ultra Adventures Steam Web API reviews: this request returns {"success":1,"query_summary":{...},"reviews":[...]} and if you open this Steam Community page you can check that the game has reviews and they are being retrieved correctly.
UFO: Afterlight - Old Version: this returns {"success":2} but when you open the game's Steam Community page you can see that the game has several reviews but they are not being retrieved.
I have not found any information on this response code. I do not think this matters but I am using two Python libraries: Requests and BeautifulSoup.
Edit:
I have found this unofficial website that has response codes. It seems to be a generic error and I still do not know how to access some app reviews.
Answer: UFO: Afterlight - Old Version was removed from Steam store.
You will get same response with any other game Title that has been removed from Store.


Java save as dialog box to save a file at a path

Question: I have a form that takes a PDF file and submits to servlet. After reading the content of PDF I am creating a xls file in servlet using Apache POI that I want to save to my local disc.
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("Data");

            ArrayList<String> keys = new ArrayList<String>(excelData.keySet());

            int counter = 0;

            for(int i=keys.size()-1; i>=0;i--){

              String key = keys.get(i);
              String value = excelData.get(key);

              Row row = sheet.createRow(counter++);

              Cell cell = row.createCell(0);
              cell.setCellValue(key);

              Cell cell1 = row.createCell(1);
              cell1.setCellValue(value);
            }

//work book is the xls data that currently I am saving like ,
                FileOutputStream out = new FileOutputStream(new File("C:\\Users\\user\\Desktop\\new.xls"));
                workbook.write(out);
                out.close();

                //return back to same page.
                response.sendRedirect(request.getHeader("Referer"));

Instead of saving file directly to C:\\Users\\user\\Desktop\\new.xls,I want  to give a save as option to select the path and name of the file that user want to save. Some thing like :

How can I send this file back to the client browser and show save as option.
Answer:                 //read file back after saving to some temp path.
                File pdfFile = new File("D:\\temp\\"+ fileName +".xls");

                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-Disposition", "attachment; filename="+fileName+".xls");

                FileInputStream fileInputStream = new FileInputStream(pdfFile);
                OutputStream responseOutputStream = response.getOutputStream();
                int bytes;
                while ((bytes = fileInputStream.read()) != -1) {
                    responseOutputStream.write(bytes);
                }

                fileInputStream.close();


What to do with loading animation for very short load times?

Question: Within our iOS mobile app, we have a list of templates the user can choose from:

Each template has (initially) remote assets that need to be downloaded and cached before the user can go into edit-mode.
If the user tapped on a template, two things could happen:

If all the template assets are already cached - just go straight to the edit-mode screen.
If one or more assets aren't cached - we present a modal that showing the "fetch progress". Once it's done download & cache - we go into the edit-mode screen.

The issue is that sometimes fetching the asset of the template could take 0.1/0.2 seconds. Which is far shorter than the modal presentation animation (takes around 0,5).
Here's the UI we designed for the modal:

My questions here:

Is it appropriate to dismiss the modal without animation when the fetch completed?
Is there a best practice for this kind of scenario, where the modal could be dismissed while it's presenting itself (during the presenting animation)?
Will using a modal other than a full-screen one is more appropriate?

UPDATE:
Here is the outcome of your answers. What do you think?
(This showcasing a device with a fast internet connection)
Answer: There are a couple of ways of dealing with this issue.
1. Use placeholders
Personally, I find this to be the best solution. On click of the user, move to the next screen with placeholders and lazy loading implemented. As soon as an asset gets downloaded, it would replace the placeholder. That eliminates the problem of loading animation altogether. User testing has also shown that people feel like the load times are lesser when they see such placeholders instead of loading animations
2. The placebo loading animation
Through multiple user testings and reviews, it has been found that users tend to expect some sort of a delay/loading time when there is a major transition or if data on the page is getting updated. You might have heard of the "pull to refresh" action that many apps have nowadays. Most of them actually do nothing. The page has a refresh timer but the "pull to refresh" is present to give user a feeling of control and satisfaction. (Yes, it is actually there to trigger a request of data from the server in case of loss of connection or a timeout, but in many cases it is used as a placebo)
So, if your loading animation is 0.5 secs long, let it complete the animation even if there's no loading time or if the loading time is shorter.
3. Indicate which items are "ready"
It might add the burden of coding in that logic but if a certain template was opened and its assets were cached, add a border or corner notch to indicate that the template is ready to be opened.
That way, you can simply remove the loading animation for those items. And in case the cache gets cleared and the assets need to be downloaded, let the request trigger the animation.
4. Put the loading animation on the next screen
It does two things. First, it gives the user an immediate feedback for the action they performed. Second, it gives you the "page transition time buffer". Ideally, a page transition would be under half-a-second long. If the data gets downloaded in that time, no need to trigger the loading animation. And the default state of the next screen would be to carry the loader so that if the loading time is longer than the transition, the user would see the loading animation
Comment: How do you know that some "pull to refresh" do nothing?
Comment: I read about it in an article where a lot of sports apps add it in because the users get itchy when the game is at a critical juncture and like to refresh often as a nervous tick. I'll add the link to it if I can find it
Comment: Sorry for the delay, was flying aboard. Basically, you are correct, doing it at the same screen creates too much of a hassle. Number 4# did the tricks. And that's what we decided to too: Basically, when the user taps on a template, we already know if the templates assets need to be downloaded or not. If we find that there are missing assets, we are doing the normal "push animation", but inside the edit-canvas screen, we show the "loading modal" in the bottom, immediately, without animation. We found that that sometimes the download could finish even while the "push animation" occurring.
Comment: So now as I see we have two options:
1. Fetch the assets and dismiss the modal screen, even if it's a little bit flashy (as sometimes it's super fast) 2. Only show the modal when the "push animation" finished. Number two creates somewhat of a "state" to the app (code-wise) where Number one creates a much more atomic logic. But might be a little flashy. What do you think?
Comment: Please see the updated question, I've included the result with a GIF :)
Answer: I agree with Shreyas Tripathy's answer. I would personally pick #4 for your case and then use one of other activity indication on the next screen if needed. If your usual screen transition time is less than loading time of the image the delay won't be perceivable in most cases.
Just want to add that using a modal sheet is not an appropriate mechanism here for two main reasons.

The in and out transitions add to the waiting time, ultimately making it even slower.
Modal presentation by its nature and purpose catches attention. In your case it adds a lot of friction to the interface where friction is not needed, and focuses user attention of the fact of loading. Besides, your particular implementation has a close button, which is confusing. Can you close it? What would it mean?
Comment: Hey Davyd. Nice observations. First, I'd like you to read the comments I gave on Shreyas answer. About your other points: 1. I agree about the awaiting time. Please note that when users tapped on a template it'll just do the regular "animation push" and will add the modal without animation (in the pushed view controller), so it'll look just part of it. 2. It's not ideal but our entire app "modal-alerts" presentation is the same, and we thought it's important to keep it consistent. Moreover, for low internet speeds, we expect this screen to have a longer lifetime.What modal would you recommend?
Comment: For the close button, it'll simply "pop" the view controller back, basically, if the user doesn't want to wait for the assets to download, it'll take him back to the templates screen. I think that X seems to be inappropriate as the user might think it'll close the modal?
Comment: Please see the updated question, I've included the result with a GIF :)


Magento 2 error after upgrade and deploy

Question: For a template in magento 2 we had to do following in SSH
Run command line: php bin/magento module:enable MGS_Mpanel
Run command line: php bin/magento module:enable MGS_Mmegamenu
Run command line: php bin/magento module:enable MGS_Portfolio
Run command line: php bin/magento module:enable MGS_Testimonial
Run command line: php bin/magento module:enable MGS_Brand
Run command line: php bin/magento module:enable MGS_Core
Run command line: php bin/magento module:enable MGS_Promobanners
Run command line: php bin/magento module:enable MGS_StoreLocator
Run command line: php bin/magento module:enable MGS_Blog
Run command line: php bin/magento module:enable MGS_QuickView
Run command line: php bin/magento module:enable MGS_AdvancedReports
Run command line: php bin/magento setup:upgrade
Run command line: php bin/magento setup:static-content:deploy
Magento was installed correctly, but after deploy we get following error:
a:4:{i:0;s:294:"Warning: include(/home/[myusername]/domains/[mydomain]/public_html/var/generation/Magento/Framework/App/FrontController/Interceptor.php): failed to open stream: No such file or directory in /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/Code/Generator/Io.php on line 158";i:1;s:2288:"#0 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/Code/Generator/Io.php(158): Magento\Framework\App\ErrorHandler->handler(2, 'include(/home/d...', '/home/[myusername]/...', 158, Array)
1 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/Code/Generator/Io.php(158): include()
2 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/Code/Generator.php(202): Magento\Framework\Code\Generator\Io->includeFile('/home/[myusername]/...')
3 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/Code/Generator.php(92): Magento\Framework\Code\Generator->shouldSkipGeneration('interceptor', 'Magento\Framewo...', 'Magento\Framewo...')
4 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/Code/Generator/Autoloader.php(35): Magento\Framework\Code\Generator->generateClass('Magento\Framewo...')
5 [internal function]: Magento\Framework\Code\Generator\Autoloader->load('Magento\Framewo...')
6 [internal function]: spl_autoload_call('Magento\Framewo...')
7 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/Code/Reader/ClassReader.php(19): ReflectionClass->__construct('Magento\Framewo...')
8 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/ObjectManager/Definition/Runtime.php(44): Magento\Framework\Code\Reader\ClassReader->getConstructor('Magento\Framewo...')
9 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(71): Magento\Framework\ObjectManager\Definition\Runtime->getParameters('Magento\Framewo...')
10 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/ObjectManager/ObjectManager.php(71): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\Framewo...')
11 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/App/Http.php(114): Magento\Framework\ObjectManager\ObjectManager->get('Magento\Framewo...')
12 /home/[myusername]/domains/[mydomain]/public_html/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
13 /home/[myusername]/domains/[mydomain]/public_html/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
14 {main}";s:3:"url";s:1:"/";s:11:"script_name";s:10:"/index.php";}
Any ideas on how to solve this have no clue and tried re-installing this for a few times now.
Comment: The error in the front end:

Warning: include(/home/[username]/domains/[mydomain]/public_html/var/generation/Magento/Framework/App/FrontController/Interceptor.php): failed to open stream: No such file or directory in /home/[username]/domains/[mydomain]/public_html/vendor/magento/framework/Code/Generator/Io.php on line 158
Answer: Hope this will help you.
Please remove var/di/* and var/generation/* using following command from your Magento project's root directory
<code>rm -Rf var/di/* var/generation/*
</code>


What am I doing wrong? Visudo

Question: I am trying to make it so that I can have a script running on my computer that will check to see if a reboot code has been sent, and it will be checking a MySQL database. I have the php script written, but when I run it, it says that to reboot I need to be root.
PHP: shell_exec("/sbin/reboot");
I have tried to edit visudo and added this to bottom of the file:
ALL ALL = NOPASSWD: /sbin/reboot

It still doesn't work. What do I need to do differently?
Comment: You have to be specific to create titles of questions here on StackExchange.

Your title looks selfish and isn't going to help anyone that have the same problem as you, because he's not going to find this question.

Note: I'm just trying to explain the comunity rules. ^^ Welcome to ask ubuntu.
Answer: I just figured it out, but I thought I'd put it here in case anyone else needs help. In the PHP script, I needed to run it as sudo:
shell_exec("sudo /sbin/reboot");

Then it doesn't ask for the password and works fine.
Comment: 5 minutes to figure out the answer? XD
Comment: I had been working on it for the last day or so and thought I wasn't going to be able to get it to work. That's how computer stuff goes...you try 1000 things, and you don't know if you'll get it right on try 17 or 999.
Comment: I know what you mean. :)

What am I doing wrong? Visudo


How to add contitions to Homeassistant App Widget?

Question: I have a homeassistant android widget using the html template format:
{{ states('sensor.mystate') }}

How can I change the color conditionally based on the value of the sensor?
Is it possible to add if/else conditions into the widget html template somehow?
Answer: {% if states('sensor.mystate')|int > 75 -%}
  {% set color = 'green' %}
{%- else -%}
  {% set color = 'red' %}
{%- endif %}
{{ color }}

{{ states('sensor.mystate') }}


How to avoid the glitch in Gerimund's Hall in Forbidden Legend quest?

Question: I could not retrieve Galdur's Amulet in Gerimund's Hall when I first killed the brother (Sidgis?) because it seemed to be stuck underwater with no option to retrieve it.
How to solve this?
Answer: As far as what causes the glitch in the first place, I am not sure if playing through Gerimund's Hall BEFORE you start the Forbidden Legend Quest, causes the amulet to get stuck under water with no option to retrieve it, BUT I found how to avoid it. It's all about where he lands after he is defeated.
I did have to play through it a second time when I reloaded the game from before I fought him, and happened to defeat Sidgis on land, near his coffin, so that he did not fall in the water, and was able to retrieve the amulet off of him and further the quest.
So I would save it right BEFORE you fight him, in case he lands in the water, and it glitches, so that you can load it again from immediately before the fight. Be aware, you will have to defeat him again at Reachwater Rock, when you go there to re-forge the amulet.
Comment: You can always post an answer if you have solutions, you don't need to leave a comment to other posts to do that. And please, refrain from begging for reputation, you should deserve it :)


Regex for custom email validation

Question: Trying to create custom email validation for below rules

The local part can be up to 64 characters in length and consist of any combination of alphabetic characters, digits, or any of the following special characters:
! # $ % & ' * + – / = ? ^ _ ` . { | } ~
The period character (".") is valid for the local part subject to the following restrictions: A. it is not the first or last character
B. two or more consecutive periods
top level domains cannot be all numeric
hyphens cannot be the first or last character

^([a-zA-Z0-9!#\$%&'*+/\=\?\^_'`}{\|~-][.]?)@[a-zA-Z0-9]+(?:(.)\0?(?!\1))[a-zA-Z0-9-]*[a-zA-Z0-9]+(.[a-zA-Z0-9]{2,63})+$

<code>First part (before @ )is good but unable to place
</code>

two or more consecutive periods
hyphens cannot be the first or last character
for example
email@example.com(correct)
leela.test@te-st..gm-ail.com(incorrect)
leela.test@.te-st.gm-ail.com(incorrect)
leela.test@-te-st.gm-ail-.com(incorrect)
leela.test@.te-st.gm-ail-.com(incorrect)
email@example.com(correct)
firstname.lastname@example.com(correct)
firstname.lastname@example.com(correct)

Please help.
Comment: There are already a number of email regexes here, but here is an attempt at your specific case: https://regex101.com/r/uQMH2p/1
Answer: <code>[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*\@[a-zA-Z0-9]+\-[A-Za-z0-9]+\.[a-zA-Z0-9]+\-[A-Za-z0-9]+\.com
</code>
Comment: Please explain this solution
Comment: @revaljilji, This is also need to match for firstname.lastname@example.com, firstname.lastname@example.com, firstname.lastname@example.com

Regex for custom email validation


How to display a text without the whitespaces in Xaml textbox

Question: I get an input from a different view model, and i need to show it in another window without the white spaces inserted. But it should not replace the original text. I need the white spaces removed only when displayed
Comment: @Gethma if it is just for display, then one-way binding with a converter can do the trick. For example, if the property in the viewmodel is "Hello World", you can bind to the property and display "HelloWorld" in the View with a value converter that removes the white spaces out of the original string. And since it's one way binding you don't have to worry that the View modifies the property in the ViewModel.
Comment: Are you databinding to the model property or are you setting it via `.Text = `?
Comment: Consider using Value Converter.
Comment: @kennyzx the value converter will change the original text. It there any other way we can solve in the Xaml code level. Without converting?
Comment: @Gethma Yes to which question?
Comment: Are you databinding to the model property or are you setting it via .Text = ? Yes i do @johanP
Answer: Here you need Converter to trim your text like this:
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

[ValueConversion( typeof( string ), typeof( string ) )]
public class NoWhiteSpaceTextConverter : IValueConverter
{
    public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
    {
        if ( ( value is string ) == false )
        {
            throw new ArgumentNullException( "value should be string type" );
        }

        string returnValue = ( value as string );

        return returnValue != null ? returnValue.Trim() : returnValue;
    }

    public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
    {
        throw new NotImplementedException();
    }
}

And use converter with text binding in xaml like this:
<Windows.Resources>
     <converter:NoWhiteSpaceTextConverter x:Key="noWhiteSpaceTextConverter"></converter:NoWhiteSpaceTextConverter>
</Windows.Resources>

<TextBox Text="{Binding YourTextWithSpaces, Converter={StaticResource noWhiteSpaceTextConverter}}" />

Comment: It is optional but good practice to understand converter better: see this answer https://stackoverflow.com/a/9628979/2106315
Comment: [ValueConversion( typeof( string ), typeof( string ) )] what does this line mean?


getting a hidden element and sending it to a different URL

Question: I'm doing a challenge which requires me to get an ID of element (from the source code - its hidden there - in my example called token), and then re-send it to another page with POST request included. however the code will not keep going to the 2nd request(the POST one) some why and it won't throw an error on the console.
<code>var req = new XMLHttpRequest();
req.onload = handleResponse;
req.open('get','/example/ex1',true); // will open the page /example/ex1
req.send(); // sends the request
function handleResponse() {
    var token = this.responseText.match(/name="token" value="(\w+)"/)[1];
    var changeReq = new XMLHttpRequest();
    changeReq.open('post', '/example/ex2', true);
    changeReq.send('token='+token+'&user=admin')
};
</code>
I appreciate your help.
Comment: no error on the console, but what does your troubleshooting tell you? putting in console.log statmetns to check values, checking the network tab in your web inspector.... There are a myriad of ways to troubleshoot your own code besides error statements
Answer: check the network tab. make sure the target URL is called.


What is the recommended strategy of complete refactoring of a live product?

Question: Consider that you have "System_A" which is a web application. And System_A has simply 3 layers: UI, BusinessLayer and Data-Layer.
And now you want to make a widespread refactoring of System_A (the UI and BusinessLayer only) while it is working as a live product.
What should be the safest strategy of refactoring System_A and release the refactored product after a good testing -let's call it "RF_SystemA"- in a way that it can be reversed to System_A even in case of an unexpected bug (without forcing the users to change their URLs)?
Answer: First of all, you should have enough unit tests to cover the critical parts of the code. (If the UI and business logic is not separated well, making the logic difficult to test, you may need to lean more on functional/integration tests until you separate the layers enough to make the business logic unit testable.)
You should also have test/staging server(s) apart from the production one(s), where testers and users can verify that the new code is working well.
Then you can make small changes in the code, step by step, verifying after each step that all unit tests still pass.
After major milestones, you deploy the refactored app to the test/staging servers and run larger scale integration/functional/performance/whatever else you have tests.
If after all tests, you are happy and confident that the code still works as expected, you may deploy it in production.
Should a bug pop up after all this, you can easily return to the latest safe version in production. (And after that, you can start investigating how the bug slipped through and how to avoid this happening again...)
Comment: Thanks PEter. Question: Do you think it is a good idea to have a copy of the System_A's parts and do all the changes on it. So you keep both refactored and old system's code for some time and it gives you an advantage of seeing what was changed more easily? And after some time you can delete the old system's code completely.
Comment: @burak, this is what SCM is for. I assumed you are using source control - if you don't, start with that. There are several excellent open source solutions like Subversion, Git or Mercurial - pick one. These also make it possible to use a separate _branch_ for the refactoring while adding new features on the main branch - although I don't recommend this in the longer term. But when used with specific short term goals, it can be a good solution.
Comment: Peter, I am familiar with svn. But I think I could not express my need clearly. Nonetheless, B3ret's answer was what I was looking for to hear. Thank you for your recommendations as well.
Answer: I haven't much to do with Web-Development, but just an idea:
You could write/generate a redirect-Layer first like so:
Let's say you had three functions in System_A like this:
System_A.Call1(...)
System_A.Call2(...)
System_A.Call3(...)

Then you could rename the calls like this...
System_A.Call1(...) -> Old_System_A.Call1(...)
System_A.Call1(...) -> Old_System_A.Call2(...)
System_A.Call1(...) -> Old_System_A.Call3(...)

...and create a temporary redirect system like this:
System_A:Call1(...) { Old_System_A.Call1(...); }
System_A:Call2(...) { Old_System_A.Call2(...); }
System_A:Call3(...) { Old_System_A.Call3(...); }

This system could go live immediatly.
Then you could create your Refactoring classes bit by bit, test them and just change the redirection calls.
After refactoring is complete, you remove the redirection calls and just use the reafactored system.
The granularity of your redirection should correspond to the granularity of your refactoring. So if you refactor individual functions and the function signatures stay the same, you can use the approach as stated above.
If you completely redesign whole parts of your system, you could redirect the whole parts.
Example for web:
You have index.php which mainly calls 3 functions: header(), body() and footer().
You could either redirect the calls to header, body and footer (if the signatures stay the same), or you could redirect the whole thing and call only one function from your index.php like "currentIndexPhp()";
currentIndexPhp() would then initially call header, body and footer.
If you organize your folders and files accordingly, you should be able to control the refactoring quite nicely.
Comment: Ok the above answer is definitely the better one. Mine is like for the poor man's small webproject ;-)
Comment: the potential problem with this is that it lends itself very easily to "patching" by changing redirections on the fly back and forth. This can lead to chaos, when noone anymore can remember for sure whether a specific functionality of the system is using the old or new implementation in production. Also, this can lead to a combinatorial explosion of system versions to test. So it requires _very_ disciplined developers to do it right. Or, as you say, a small project. Btw thanks for the compliment :-)


Sending a file in jquery ajax POST Api call

Question: Using Jquery, when i try to call external API by sending any file. I'm facing 415 Unsupported Media Type error. I'm trying to send my document using FormData() and header data in beforeSend() function. Suggest me correct way of sending data.

  <input type="file" id="file-select" name="photos" />
  <button id="myButton" value="click me">click me</button>

$("#myButton").click(function() {
  var myData = new FormData();
  var myFile = document.getElementById("file-select");
  myData.append(myFile.files[0].name, myFile.files[0]);

  $.ajax({
    url: 'http://10.96.45.179/RightFax/API/attachments',
    data: myData,
    processData: false,
    beforeSend: function(xhr) {
      xhr.setRequestHeader('Authorization', 'Basic c3lzYWRtaW46cGFzc3dvcmQtMQ==');
    },
    type: 'POST',
    success: function(result) {
      console.log(result);
       return result;
    }
  });
});

Query 2: If my document is present in URL. please let me know How can I pass the URL as a file source.
for Example:
filepath = 'http://seekvectorlogo.com/wp-content/uploads/2018/02/opentext-vector-logo-small.png';

I need to pass this file source to my external API.
Comment: When uploading binary data in a `FormData` object you need to set `contentType: false`
Comment: Thanks! It resolved my error.
Comment: Glad it helped. I added it as an answer for you below
Answer: When uploading binary data in a FormData object you need to set contentType: false:
$.ajax({
  url: 'http://10.96.45.179/RightFax/API/attachments',
  data: myData,
  processData: false,
  contentType: false,
  // your other settings...
});


Python improve import time of data

Question: I have a data file that contains the following:
somename = [ [1,2,3,4,5,6],...plus other such elements making a 60 MB file called somefile.py ]

In my python script (of the same folder as the data) I have (and only this along with appropriate shebang):
from somefile import somename

This took almost 20 minutes to complete. How can  such an import be improved?
I'm using python 3.7 working on a mac osx 10.13.
Comment: why not use something like a json file for static data? and have it loaded when it is needed.
Comment: From your question it seems your entire file is raw data and not actual python implementations. Is this the case?
If so treating it as a text file and reading its lines could dramatically improve performance
Comment: @BhathiyaPerera Many thanks for the suggestion.
Comment: Quite simply: do not try to use a python module as data storage for such volumes - use a proper datastore (database, whatever).
Answer: loading files as "Python source code" will always be relatively slow, but 20 minutes to load a 60MiB file seems far too slow.  Python uses a full lexer/parser, and does things like tracking source locations for accurate error reporting amongst other things.  It's grammer is deliberately simple which makes parsing relatively fast, but still it's going to be much slower than other file formats.
I'd go with one of the other suggestions, but thought it would be interesting to compare timings across different file formats
first I generate some data:
somename = [list(range(6)) for _ in range(100_000)]

this takes my computer 152 ms to do, I can then save this in a "Python source file" with:
with open('data.py', 'w') as fd:
    fd.write(f'somename = {somename}')

which takes 84.1 ms, reloading this using:
from data import somename

which takes 1.40 seconds — I tried with some other sizes and the scaling seems linear in array length which I find impressive.  I then started to play with different file formats, JSON:
import json

with open('data.json', 'w') as fd:
    json.dump(somename, fd)

with open('data.json') as fd:
    somename = json.load(fd)

here saving took 787 ms and loading took 131 ms.  Next, CSV:
import csv

with open('data.csv', 'w') as fd:
    out = csv.writer(fd)
    out.writerows(somename)

with open('data.csv') as fd:
    inp = csv.reader(fd)
    somename = [[int(v) for v in row] for row in inp]

saving took 114 ms while loading took 329 ms (down to 129 ms if strings aren't converted to ints).  next I tried musbur's suggestion of pickle:
import pickle  # no need for `cPickle` in Python 3

with open('data.pck', 'wb') as fd:
    pickle.dump(somename, fd)

with open('data.pck', 'rb') as fd:
    somename = pickle.load(fd)

the saving took 49.1 ms and loading took 128 ms
The take-home message sees to be that saving data in source code takes 10 times as long, but I'm not sure how it's taking your computer 20 minutes!
Answer: The somename.py file is obviously created by some piece of software. If it is re-created regularly (i.e., changes often), that other piece of software should be rewritten in such a way to create data that is more easily imported in Python (such as tabular text data, JSON, yaml, ...). If it is static data that never changes, do this:
import cPickle
from somefile import somename

fh = open("data.pck", "wb")
cPickle.dump(somename, fh)
fh.close()

This will serialize your data into a file "data.pck" from which it can be re-loaded very quickly.

Python improve import time of data


document.cookie not storing cookie?

Question: Does document.cookie only work on a server? Alert doesn't seem to be outputting my cookie.
document.cookie = "d=e";
alert(document.cookie);

Comment: Are you running it from `file` protocol?
Answer: Some browsers won't honor cookies when set by a document with the file protocol.

Chrome intentionally disables cookies
  on file://, for a variety of reasons.
  When we  wrote our cookie support, the
  correct behavior was unclear when
  dealing with things  like file shares.
  We found cookies on file:// to be
  fairly uncommon, and it has a  few
  negative security implications.
You can, however, enable them with the
  command line flag
  --enable-file-cookies.
Cookies are strictly a HTTP
  mechanism as per RFC 2109. There
  should be no reasonable expectation
  for them to work for protocols other
  than HTTP, much less file:///, and it
  is not clear what their behavior
  should be, and what rules of security
  compartmentalization should apply.

Source.
Comment: chrome.exe --enable-file-cookies doesn't solve this problem, at least not any more.
Answer: Cookies are associated with a particular domain.  If you just open your HTML document as a file, then how is the browser to know what domain to associate a cookie with, or what cookies it should allow that file to access?
So yes, for all practical purposes, your browser will only allow cookies to be stored and retrieved if your HTML document is served by a server/accessed via an HTTP URL that allows it to determine some sort of domain to associate the cookie(s) with.

document.cookie not storing cookie?


Wrong TypeDescriptor when using JoinColumns in a composite Key

Question: I've been banging my head around, trying to figure what's wrong with the following mapping. I understand the following mapping is not ideal for ORM, but that's how the database is and I cannot change its structure. I'm using JPA 2.1 and Hibernate 5.0.2.Final.
@MappedSuperclass
public abstract class BaseEntity<T extends Serializable> implements Serializable {

    protected T id;

    @Id
    public T getId() {
        return id;
    }

    protected void setId(T id) {
        this.id = id;
    }
}

@Table(name = "campaign")
@AttributeOverride(name = "id", column = @Column(name = "campaign_id") )
public class Campaign extends BaseEntity<String> {
    // attributes, getters and setters
}

@Embeddable
public class CampaignBroadcastPK implements Serializable {

    @ManyToOne
    @JoinColumn(name = "campaign_id", insertable = false, updatable = false)
    private Campaign campaign;

    @Column(name = "broadcast_date")
    private LocalDate broadcastDate;

    // getters and setters
}

@Entity
@Table(name = "campaign_broadcast")
public class CampaignBroadcast implements Serializable {

    @EmbeddedId
    private CampaignBroadcastPK id;

    // attributes, getters and setters
}

@Embeddable
public class CampaignBroadcastProcessPK implements Serializable {

   @ManyToOne
   @JoinColumns({
       @JoinColumn(name = "campaign_id", insertable = false, updatable = false),
       @JoinColumn(name = "broadcast_date", insertable = false, updatable = false)
    })
    private CampaignBroadcast broadcast;

    @Column(name = "process_date)
    private LocalDate processDate;

    // getters and setters
}

@Entity
@Table(name = "campaign_broadcast_process")
public class CampaignBroadcastProcess implements Serializable {

    @EmbeddedId
    private CampaignBroadcastProcessPK id;

    // attributes, getters and setters
}

Besides this structure, I also have a converter to handle LocalDate to java.sql.Date, which is annotated with autoApply=true.
When I try to load CampaignBroadcastProcess through entityManager.find(), hibernate tries to convert campaign_id to Date, even though it's mapped as a String, causing java.sql.Date to throw an IllegalStateException, because the String passed to valueOf isn't a valid date String.
I'm suspicious Hibernate is mixing up the types of the JoinColumns on CampaignBroadcastProcessPK, handling them both as a LocalDate.
Has someone ever faced this issue?
Answer: I'm not exactly sure why, but changing the order of the two @JoinColum solved the issue. The mapping now is as below:
@ManyToOne
@JoinColumns({
    @JoinColumn(name = "broadcast_date", insertable = false, updatable = false),
    @JoinColumn(name = "campaign_id", insertable = false, updatable = false)
})
private CampaignBroadcast broadcast;

Comment: That's actually what I was saying - misordered implicit mapping. Consider using explicit bindings rather than relying on implicit order coming from the annotations
Comment: No, it wasn't the case, since even with referencedColumnName the mapping didn't work.
Answer: Try to add explicit referencedColumnName to your @JoinColumn. The error you described may be due to misordered implicit mapping of composite key columns
@ManyToOne
@JoinColumns({
    @JoinColumn(name = "campaign_id", referencedColumnName = "campaign_id", ...),
    @JoinColumn(name = "broadcast_date", referencedColumnName = "broadcast_date", ...) })
private CampaignBroadcast broadcast;

Answer: I think this is because hibernate picked the first data type which was data type and mapped it to Join column


Alsa is just not working right or I dont know

Question: I've been using linux for 5 years now but this is new for me...
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
python: src/hostapi/alsa/pa_linux_alsa.c:3636: PaAlsaStreamComponent_BeginPolling: Assertion `ret == self->nfds' failed.
Aborted (core dumped)

I can use my mic in the system like recording stuff with it and even talk in voice chat with no problem but when I try to run my speech recognition program its just fails like this...
Answer: this seems to be a known issue with portaudio where ALSA (Linux's low-level sound API) returns an error in an unexpected place
this seems to be a known issue (e.g. see here) but the authors seem to have issues with the code, and it never got added to the package
I've just stumbled on the same issue and am trying again with code that will hopefully address their issues with the previous patches
not sure if/when it would show up in Ubuntu though
Comment: I think an evolved version of this patch (or just one solving this issue for me) is here: https://stackoverflow.com/a/60824906/1509695. You also have some instructions for how to build and install it for Ubuntu there.
Comment: Not sure what to make of that bug report actually. Getting the same error on Ubuntu 18.04 (`src/hostapi/alsa/pa_linux_alsa.c:3636: PaAlsaStreamComponent_BeginPolling: Assertion `ret == self->nfds' failed`)
Comment: Can't seem to have permission to comment on the issue in PortAudio's Assembla, hope there'll be an official fix on its trunk.


How to remove a column from a file and write the result in same file using shell scripting?

Question: I have a file (a.txt) which is like below,
col1|col2|col3|col4|col5|col6|col7
a|b|c|d|m|n|o
e|f|g|h|p|q|r
i|j|k|l|s|t|u

I want to remove the col3 from the a.txt and write the result in the same a.txt file. Now the a.txt file will be look like,
col1|col2|col4|col5|col6|col7
a|b|d|m|n|o
e|f|h|p|q|r
i|j|l|s|t|u

I have done the removal portion using 'cut' command but not able to write in the same file without using 'mv' command. like - cut -d'|' -f1-2,4- a.txt
So, could anyone please help me out, how will i keep the same file with the new value.
Thanks in advance.
Comment: For all practical purposes, you have to write a new file without the column to be removed, and then move the new file over the old file.  Some commands have a way to do that for you behind the scenes (`sed -i.bak -e …` for example), but many do not.  You could do the job with `sed` instead of `cut`, but it is easier to see what's going on with `cut`.
Answer: Using awk
awk -i inplace  -F'|' '{OFS="|"; $3=""; gsub(/\|\|/,"|",$0)}1' a.txt

Demo:
$cat a.txt
col1|col2|col3|col4|col5|col6|col7
a|b|c|d|m|n|o
e|f|g|h|p|q|r
i|j|k|l|s|t|u

$awk -i inplace  -F'|' '{OFS="|"; $3=""; gsub(/\|\|/,"|",$0)}1' a.txt
$cat a.txt
col1|col2|col4|col5|col6|col7
a|b|d|m|n|o
e|f|h|p|q|r
i|j|l|s|t|u
|
$

Explanation:
-i inplace < -- Make changes to file
-F'|'  <--  Set input filed seprator as  |
OFS="|" < -- Set putput field seprator as |
$3="" <-- Set column 3 as empty
gsub(/\|\|/,"|",$0)  <--  Replace  || with |
Edit: Using sed
sed -i  's/[^|]*|//3' a.txt
Demo
$cat a.txt
col1|col2|col3|col4|col5|col6|col7
a|b|c|d|m|n|o
e|f|g|h|p|q|r
i|j|k|l|s|t|u
$ sed -i  's/[^|]*|//3' a.txt
$cat a.txt
col1|col2|col4|col5|col6|col7
a|b|d|m|n|o
e|f|h|p|q|r
i|j|l|s|t|u
$

Explanation"
$echo "a|b|d|m|n|o" | sed -e's/[^|]*|/(&)/'
(a|)b|d|m|n|o
$

[^|] -- Any charactet excep |
* -- Zero oe more occurence
3 3rd occurence
Comment: Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:          GNU long options:
        -f progfile             --file=progfile
        -F fs                   --field-separator=fs

To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.
gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.
Comment: Replace awk with gawk
Comment: Still not working.. shall we use any other command instead of -i inplace?
Comment: Try `sed -i  's/[^|]*|//3' a.txt`
Comment: Welcome. I have updated the answers explaining  the pattern match.
Comment: Hi Digvijay,

I am facing one issue for this command {sed -i  's/[^|]*|//3' a.txt}. If the delimiter is "~" instead of "|"  then it is not working. Could you please help me out from this.
Comment: Replace `|` with `~`
Comment: col1~col2~col3~col4~col5~col6~col7
a~b~c~d~m~n~o
e~f~g~h~p~q~r
i~j~k~l~s~t~u

and i have run the below command,

 sed -i 's/[^~]*|//3' a.txt

But it is not able to discard the 3rd column.

I have tried this one echo "a~b~d~m~n" | sed -e's/[^~]*|//3' also, but it won't discard anything.
Comment: Hi @user3040157 You have to replace both `|`  try `sed   's/[^~]*~//3'`
Comment: Demo `$echo 'col1~col2~col3~col4~col5~col6~col7' | sed 's/[^~]*~//3'
col1~col2~col4~col5~col6~col7
$
`


How to compare array before removing its items in SwiftUI?

Question: How can i compare original items of an array before i remove them? I have an array of items that get removed every time they are showed and get added into the empty array... to avoid index out of range i was trying: if emptyArray.count < array.count { //append element } but it is not working as expected because elements of the full array get removed from it in order to be unique, so it works until the empty array contains half the elements of the full one
func changeNumber() {
        if appearedNumber.count < numbers.count { // <-- doesn't work because he lost items
            appearedNumber.append(currentNumber)

            let index = Int.random(in: 0..<numbers.count)
            currentNumber = numbers[index]
            numbers.remove(at: index) // remove items from full array
        }
    }

Full code
struct ContentView: View {
    @State private var numbers = ["0", "1", "2", "3", "4", "5"]
    @State private var currentNumber = ""

    @State private var appearedNumber = [String]()

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink {
                    SecondView(appearedNumber: $appearedNumber)
                } label: {
                    Text("Change view")
                }
                .padding()

                Text(currentNumber)
                    .font(.largeTitle)
                    .bold()

                Button {
                    changeNumber()
                } label: {
                    Text("Change person")
                        .font(.largeTitle)
                        .bold()
                }
                .padding()
            }
        }
    }
    func changeNumber() {
        if appearedNumber.count < numbers.count {
            appearedNumber.append(currentNumber)

            let index = Int.random(in: 0..<numbers.count)
            currentNumber = numbers[index]
            numbers.remove(at: index)
        }
    }
}

struct SecondView: View {
    @Binding var appearedNumber: [String]

    var body: some View {
        VStack {
            ForEach(0..<appearedNumber.count, id: \.self) { index in
                Text(appearedNumber[index])
                    .font(.largeTitle)
                    .bold()
            }
        }
    }
}

Comment: Use `ForEach(appearedNumber, id: \.self) { number in` instead of an index.
Comment: This function should do work if numbers.count > 0. And your append should happen after current assignment. Last element should be appended manually.
Comment: @JoakimDanielson is correct. Also, rather than randomizing each time you draw a number, created it like `@State private var numbers = ["0", "1", "2", "3", "4", "5"].shuffle()`. This creates a randomized array, and you can draw them out one at a time.
Answer: You can simplify this greatly by shuffling the array...
@State private var numbers = ["0", "1", "2", "3", "4", "5"].shuffled()

then:
func changeNumber() {
    if numbers.count > 0 {
        currentNumber = numbers.removeFirst()
        appearedNumber.append(currentNumber)
    } else {
        currentNumber = "out of numbers"
    }
}

Edit - in response to comment...
If you want the first random number displayed when the view appears, add .onAppear():
var body: some View {
    NavigationView {
        VStack {
            NavigationLink {
                SecondView(appearedNumber: $appearedNumber)
            } label: {
                Text("Change view")
            }
            .padding()

            Text(currentNumber)
                .font(.largeTitle)
                .bold()

            Button {
                changeNumber()
            } label: {
                Text("Change person")
                    .font(.largeTitle)
                    .bold()
            }
            .padding()
        }
    }
    // add this
    .onAppear() {
        changeNumber()
    }
}

Comment: I know it, but the question is how to compare an empty array that is getting filled with the original value of another one that is getting emptied
Comment: @simonaus -- trying to make sense of what your goal is... in your post you said: *"it works until the empty array contains half the elements of the full one"* ... well, if you change this: `if appearedNumber.count < numbers.count {` to this: `if numbers.count > 0 {` you won't have that problem. Maybe that gives you your desired result?
Comment: This works but nothing will be displayed until the user presses the button for the first time... what if i want to show one of those numbers as soon as the app is launched?
Comment: @simonaus - implement `.onAppear()` ... see the **Edit** to my answer.


Typescript: adding type definition to Observable.of(null)

Question: In my program I have a method that can return either an observable of MyClass or Observable.of(null).
The code is along the following lines:
doSomething(someParams) {
      return this.backEnd.getStuff(someParams)
                    .switchMap(data => {
                        if (!data ) {
                            return Observable.of(null);
                        } else {
                            this.doSomethingElse(data);
                        }
                    });
}

where doSomethingElse() returns an Observable<MyClass>.
If I code the method as above, I loose intellisense since my IDE (VSCode) tells me that the method doSomething() returns an Observable.
Is there a way to maintain intellisense in such a case? What I would like to have is the IDE suggesting me that the method doSomething() returns an Observable<MyClass> and naturally have my logic check is there is actually an instance of MyClass or null when the result is subscribed.
Answer: You can type your return type as Observable<MyClass | null> where MyClass | null is a union type:
doSomething(someParams): Observable<MyClass | null> {
   // ...
}


$form->isValid is always false in Zend_Form

Question: Well, i load the content from editAction in the edit.phtml using ViewScript from zend_form, but when user click on Save Button without change any content the line $form->isValid() return always null.
Look all my code:
CadastroRegiao.php (this code build all components to edit.pthml page)
<?php

class Cadastros_Form_CadastroRegiao extends Zend_Form {
public function init() {

    //Captura o View atual para trabalharmos com seus métodos
    $view = Zend_Layout::getMvcInstance()->getView();
    $front_controller = Zend_Controller_Front::getInstance();

    //Array para removeção de acentos
    $caracterComAcento = array("/á/", "/à/", "/â/", "/ã/", "/ä/", "/é/", "/è/", "/ê/", "/ë/", "/í/", "/ì/", "/î/", "/ï/", "/ó/", "/ò/", "/ô/", "/õ/", "/ö/", "/ú/", "/ù/", "/û/", "/ü/", "/ç/"
        , "/Á/", "/À/", "/Â/", "/Ã/", "/Ä/", "/É/", "/È/", "/Ê/", "/Ë/", "/Í/", "/Ì/", "/Î/", "/Ï/", "/Ó/", "/Ò/", "/Ô/", "/Õ/", "/Ö/", "/Ú/", "/Ù/", "/Û/", "/Ü/", "/Ç/");
    $caracterSemAcento = array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c"
        , "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C");
    $filterPregReplace = new Zend_Filter_PregReplace();

    //Configuração e Inicialização do Componentes
    $cod_regiao = new Zend_Form_Element_Hidden('cod_regiao');
    $cod_regiao->setName('cod_regiao');

    $nome = new Zend_Form_Element_Text('nome');
    $nome->setLabel('Nome*: ')
            ->setRequired(true)
            ->addFilter($filterPregReplace->setMatchPattern($caracterComAcento)->setReplacement($caracterSemAcento))
            ->addFilters(array('StripTags', 'StringTrim', 'StringToUpper'))
            ->addFilter(new Zend_Filter_Alnum(true))
            ->addFilter(new Zend_Filter_Null(Zend_Filter_Null::ALL))
            ->addValidators(array('NotEmpty'))
            ->addValidator(new Zend_Validate_StringLength(array('min' => 2, 'max' => '40')))
            ->addValidator(new Zend_Validate_Db_NoRecordExists('regiao', 'nome'))
            ->setAttribs(array('class' => 'upper'))
            ->setName('nome');

    $submit = new Zend_Form_Element_Submit('salvar');
    $submit->setName('Salvar');

    $cancelar = new Zend_Form_Element_Button('cancelar');
    $cancelar->setName('Cancelar')
            ->setAttribs(array("onClick" => "dialogoConfirm('Cancelar','Deseja Cancelar ?','" . $view->url(array('module' => 'cadastros', 'controller' => 'regiao', 'action' => 'index')) . "');"));

    $this->addElements(array($cod_regiao, $nome, $submit, $cancelar));

    //Configuração dos Decorators
    $this->setDecorators(array(
        array('ViewScript', array('viewScript' => "regiao/" . $front_controller->getRequest()->getActionName() . "phtml"))
    ));

    $this->setElementDecorators(array(
        'ViewHelper',
        array('Errors', array('class' => 'erroValidacao')),
        'Description',
        'Label'
    ));

    $submit->removeDecorator('label');
    $cancelar->removeDecorator('label');
}

}
RegiaoController.php [editAction] (this action link the CadastroRegiao.php and edit.html)
Look: this code always return "NOT IS VALID" when user don't change any information, just let the original information to save.
public function editAction() {

    $model = new Cadastros_Model_Regiao();
    $form = new Cadastros_Form_CadastroRegiao();

    if ($this->_request->isPost()) {
        if ($form->isValid($this->_request->getPost())) {
            $model->update($form->getValues());
           $this->_redirect('cadastros/regiao');
        }else
            echo "NOT IS VALID";
    } else {
        $regiao = $model->find($this->_getParam('cod_regiao'));
        $form->populate($regiao);
        $this->view->form = $form;
    }
}

And finally, my edit.phtml

Edição de Região

        <img src="<?= $this->baseUrl(); ?>/css/imagens/close.png" width="16" height="16" title="Clique para fechar" alt="Clique para fechar" class="fechar_aviso" />
        Aviso !

            1. Todos os caracteres especiais e acentos presentes no campo Nome serão removidos.

    <form action="" enctype="multipart/form-data" method="post" name="form_cad_regiao">

        <?= $this->form->cod_regiao; ?>
        <?= $this->form->nome; ?>

            <?= $this->form->Salvar; ?>
            <?= $this->form->Cancelar; ?>

    </form>

Answer: Instead of
echo "NOT IS VALID";

Do
print_r($form->getErrors());

Comment: I changed to print_r.. as you said. Look what show:
Array ( [cod_regiao] => Array ( ) [nome] => Array ( [0] => recordFound ) [Salvar] => Array ( ) [Cancelar] => Array ( ) )
Comment: I cant read that since its another language, but I assume its saying a record doesnt exist. Take this line out: ->addValidator(new Zend_Validate_Db_NoRecordExists('regiao', 'nome'))
Comment: Yeah, this is the problem. the new Zend_Validate_Db_NoRecordExists line is doing it, but why don't show any error about that in Form ? In my newAction when the user let the input empty apper a error: "Your must fill this field".
Comment: try, $nome->setDecorators(array('ViewHelper', 'Errors'));
Comment: I'm making this:

$this->setElementDecorators(array(
            'ViewHelper',
            array('Errors', array('class' => 'erroValidacao')),
            'Description',
            'Label'
        ));
Comment: some sugestion why the Errors don't apper ?


Plane parallel to the x axis.

Question: What does it mean for a plane to be parallel to the x axis (Or any other axis) ? And if the plane is parallel to the x axis for example wouldn't it also be in parallel to the y axis ? Could someone help me understand this ?
Comment: The plane $y=1$ is parallel to the $x$-axis but intersects the $y$-axis.
Answer: Let $\vec{n}(a,b,c)$ be a мусещк normal to the plane.
Thus, since $\vec{l}(1,0,0)$ is parallel to the $x$-axis we obtain:
$$(1,0,0)(a,b,c)=0$$ or $a=0$.
Thus, we got an equation of our plane:  $$by+cz+d=0,$$ where $b^2+c^2\neq0$.
If our plane is not parallel to $y$-axis then $b\neq0$.

Plane parallel to the x axis.


finding $h$ that make absolute instability for Euler method

Question: I have an Euler method that has this form:
$$\hat{I}(t_{n+1}) = \hat{I}(t_{n})+h\beta \hat{I}(t_{n})[1-\frac{\hat {I}(t_{n})}{N}]$$
which can also be written like
$$\hat{I}(t_{n+1})=\phi (\hat{I}(t_{n}))$$
where $\phi (x)$ is the iteration function down below:
$$\phi(x) = x + h \beta x (1- \frac{x}{N})$$
I use $h=6$ in this method but if I use a $h$ which is a little bit bigger (for example $h=20$), I have an absolute instability error. I want to find the value of $h$ from which this absolute error is shown?
Thanks in advanced
Comment: The value of $h$ alone does not give any reproducible information. You need also give the value of $\beta$ that you used.
Comment: @LutzLehmann $\beta = 0.125$ in this question. Sorry for not mentionning it
Comment: Then up to $h=16$ you get convergence, and $h=20$ is inside the first bifurcation cascade.
Comment: @LutzLehmann how did you come up with $h=16$? Can I have more explination please?
Comment: Because you get convergence to a limit for $βh\in[0,2]$.
Answer: Divide the equation by $N$ to get a new equation in $x=I/N$ that does not contain $N$. Replace $k=h\beta$, so that what remains is the more simple equation
$$
x_{n+1}=x_n+kx_n(1-x_n)=x_n(1+k-kx_n)
$$
Now finally divide by $(1+k)$ and set $y=kx/(1+k)$ to get the discrete logistic map
$$
y_{n+1}=(1+k)y_n(1-y_n)
$$
You can read off the stability of that iteration from any plot of the Feigenbaum diagram.
In summary, for $k\in[0,2]$ you get convergent behavior, after that periodic solutions, looking increasingly chaotic after $k=2.82..$
Answer: If $\phi(x)$ is a contraction in
$$
x_{k+1}=\phi(x_k)
$$
then following with
$$
x_{k+1}-x_k = \phi(x_k) - \phi(x_{k-1}) = \phi'(\xi)(x_k-x_{k-1})
$$
to have a fixed point is sufficient that $|\phi'(\xi)|<1$ or
$$
-1\lt 1-h\frac{\beta x_k}{N}+h \beta\left(1-\frac{x_k}{N}\right)< 1
$$


Default constructor not getting called

Question: Why default constructor(same class) is not getting called while calling the default constructor but the default constructor of parent class is getting called - Why?
class A{
    A(){
        System.out.println("A()");
    }
}

class B extends A{
    B(){
        System.out.println("B()");
    }
}

class C extends B{
    C(){
        System.out.println("C()");
    }
    C(int i){
        System.out.println("<------>"+i);
    }
}
public class sample {
    public static void main(String[] args) {
        C c = new C(8);

    }
}

Output:
A()
B()
<------>8

Comment: Technically that's not a default constructor. In Java you get a synthetic default constructor supplied if there is no constructor in the source. A constructor with zero parameters is referred to as a *no-args* constructor. This is different from C++ where the default constructor is called by default in various situations. (The only(?) case in the Java language where a no-args constructor will be called implicitly is where a subclass constructor does not call a `this(...)` or `super(...)` explicitly.)
Answer: This is how the language works: only one constructor is called per class, unless you specifically invoke one constructor from another (like so: How do I call one constructor from another in Java?).
Comment: Thanks for response...Can you please justify your statement?
Answer: It's Java's rule. If you want your behaviour you must use this() as first instruction in C(int).
Answer: as said before it's standard behavior of java if you want some code to be always called on construction of an object you can use an initializer
class A{
    {
       System.out.println("A()");
    }
    A(){

    }
}

Answer: Based on your class declaration for class 'C', you are overloading the constructors and thus when you create a new 'C' object and pass in an integer with the following code:
C c = new C(8);

You are calling the constructor
C(int i){
    System.out.println("<------>"+i);
}

instead of the constructor
C(){
    System.out.println("C()");
}

therefore it doesn't print out "C()". Overloading constructors/functions depends on the type and number of parameters being passed in. On top of that, only 1 constructor gets called for each object being created.

Default constructor not getting called


recursion - data structure course - print all possible series

Question: I need to print all possible series that their sum is equal to N;
for example is n == 4 the output should be:
[1, 1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[1, 3]
[2, 1, 1]
[2, 2]
[3, 1]
[4]

My way of thinking to solve this problem was:
print the series that the number i is not in the series
print the series that the number i is in the series, now need to find the sum of N-i.
my code:
#include <stdio.h>
#include <stdlib.h>

void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {

        printf(" %d ", arr[i]);
    }
    printf("\n");

}

void printAllHelper(int* a,int size, int sum, int used,int index) {
    if (sum == 0) {
        a -= used;
        printArr(a, used);
    }
    else if (sum < 0 || index == size)
    {
        return;
    }
    else {
        for(int i = 1 ; i <= size ; i ++)
        {
            printAllHelper(a, size, sum, used, index + 1);
            if (i <= sum)
            {
                *a = i;
            }
            printAllHelper(a+1, size, sum -i, used +1, index + 1);

        }

    }

}

void printAll(int num) {
    int* myArray = (int*)malloc(num * sizeof(int));
    printAllHelper(myArray,num,num,0,0);

}

void main() {
    printAll(4);
}

my output:
 3  1
 3  1
 3  1
 3  1
 3  1
 3  1
 3  1
 3  1
 3  1
 4
 1  3
 4
 2  2
 4
 3  1
 4
 4
 1  3
 1  1  2
 1  3
 1  2  1
 1  3
 1  3
 1  3
 4
 1  3
 4
 2  2
 4
 3  1
 4
 4
 2  2
 2  1  1
 2  2
 2  2
 2  2
 2  2
 4
 1  3
 4
 2  2
 4
 3  1
 4
 4
 3  1
 3  1
 3  1
 3  1
 3  1
 4
 1  3
 4
 2  2
 4
 3  1
 4
 4
 4
 4

Please try to explain to me your way of thinking, and how you approach this kind of problem, I want to be the very best like no one ever was :(.....
Answer: Your reasoning is not quite correct, but your code is almost right. The loop in your else part should be
    for(int i = 1 ; i <= sum ; i ++)  {
        *a = i;
        printAllHelper(a+1, size, sum-i, used+1, index+1);
    }

With this, I get the output
 1  1  1  1
 1  1  2
 1  2  1
 1  3
 2  1  1
 2  2
 3  1
 4

The idea is basically: "The numbers sum to sum if the first number i is any number from 1 to sum and the rest of the numbers sum to sum - i."
Also, note that your code shows some room for improvement, e.g. the used and index variables seem a bit redundant. And with not adding numbers larger than sum or smaller than 1, the check whether sum < 0 || index == size is not necessary, either. Thus you also do not need the size parameter. Your printAllHelper could be simplified to something like this:
void printAllHelper(int* a, int sum, int index) {
    if (sum == 0) {
        printArr(a, index);
    } else {
        for(int i = 1 ; i <= sum ; i++)  {
            a[index] = i;
            printAllHelper(a, sum-i, index+1);
        }
    }
}

(Note: C is not my native language, if you see more things to improve, please comment.)
Comment: Thank you so much for the clear answer!
I understand what you did there hope the next time I will be able to do this by myself.
This kind of questions are so hard to me :/


Steam Shared Library only showing Buy Option even with all shared personnel offline

Question: I recently had a friend share his library so I could play middle earth shadow of war. I downloaded it fine, but the button to play is only showing "Buy" instead of "Play". the current sharer is offline, and all people he is game sharing with are offline. Whenever I press "Play Game" from Right-click menu on the game, it says "An error occurred while updating Middle-earth Shadow of war (Shared Library Locked)" What is my problem?
This isn't a duplicate to  This Post  Because His problem consisted of the original sharer being online, mine is offline and nobody else he is sharing with is online.
Answer: The Knowledgebase says:

Shared library locked
This is an error indicates that someone else is using the shared library. A Steam library can only be used by one user at a time to play one game at a time. The same is true if that library is being accessed by another user via Family Sharing.

Most likely someone is playing in Offline mode and does not appear to be using it, but is. Not 100% sure if it's possible to be offline on chat and playing a family-shared game, but I'd guess so. Your friend who is sharing their library could launch a game and wait 2-3 minutes before exiting to kick them off in case it's running in the background accidentally or from a bug.
Answer: I've seen this bug multiple times; in every case, restarting Steam has fixed the issue.
It's also possible that he's "online but invisible", or that another family member under a different username is using the shared library.


I cant find how to build a good pygame collision system

Question: so i have a problem with my collisions. My code is kinda funky because its for a gamejam in a time of 2 days and i havent got to sorting it yet. so i want the code to print something if the honey touches the player. i can work my way up from there. here is the code
i will attach the imagesenter image description here
import pygame
from pygame.locals import *
from random import randint

pygame.init()
bgImg = pygame.image.load("background.png")
beeImg = pygame.image.load("bee.png")
honeyImg = pygame.image.load("honey.png")

#game variables
screen_width = 720
screen_height = 484
clock = pygame.time.Clock()
last_hun = pygame.time.get_ticks()
hun_frequency = 0.5 * 1000 #milliseconds
fps = 60

honeySpeed = 6

class Bee(pygame.sprite.Sprite):
    def __init__(self, x, y, alive, speed, score):
        pygame.sprite.Sprite.__init__(self)
        self.alive = alive
        self.speed = speed
        self.score = score
        self.img = beeImg
        self.rect = self.img.get_rect()
        self.rect.center = [x, y]
        self.x = x
        self.y = y
    def draw(self):
        #DRAWING
        screen.blit(self.img, (self.x, self.y))
        # MOVEMENT
        key_input = pygame.key.get_pressed()
        if key_input[pygame.K_LEFT]:
            player.x -= player.speed
        elif key_input[pygame.K_RIGHT]:
            player.x += player.speed

class Honey(pygame.sprite.Sprite):
    def __init__(self, x, y,speed):
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.speed = speed

        self.image = honeyImg

        self.image = pygame.transform.scale(honeyImg, (60,60))

        self.rect = self.image.get_rect()
        self.rect.center = [x,y]

    def update(self):
        screen.blit(self.image, (self.x,self.y))
        self.y += 5

        if self.y > screen_height:
            self.kill()

player = Bee(screen_width/2-50, screen_height/2 +120, True, 8, 0)

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Bee on the Run!')

honeyGroup = pygame.sprite.Group()
playerGroup = pygame.sprite.Group()
playerGroup.add(player)

running = True
run = True
while run:
    clock.tick(fps)
    time_now = pygame.time.get_ticks()
    if time_now - last_hun > hun_frequency:
        newHoney = Honey(randint(0, screen_width), -50, honeySpeed)
        honeyGroup.add(newHoney)
        last_hun = time_now

    #draw pics
    screen.blit(bgImg,(0,0))

    hits = pygame.sprite.spritecollide(player, honeyGroup, False)
    if hits:
        print("touched")
    player.draw()
    #newHoney.draw()
    honeyGroup.draw(screen)

    honeyGroup.update()

    hits = pygame.sprite.spritecollide(player, honeyGroup, False)
    if hits:
        print("touched")

    #event controll
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.display.update()

pygame.quit()

Comment: Why do you check for sprite collision twice?
Comment: If image is still, you probably need a _mask_ image (an image that corresponds to the honey, but where pixels of honey are white and other are black, for instance).

Then you test whether if there is any white mask pixel where your character is
Answer: I recommend to use Pygame mask collision (see also Can't figure out how to check mask collision between two sprites). All you need to do is to use pygame.sprite.collide_mask():

Tests for collision between two sprites, by testing if their bitmasks overlap (uses pygame.mask.Mask.overlap()). If the sprites have a mask attribute, it is used as the mask, otherwise a mask is created from the sprite's image.

Change in your code:
hits = pygame.sprite.spritecollide(player, honeyGroup, False)
hits = pygame.sprite.spritecollide(player, honeyGroup, False, pygame.sprite.collide_mask)

However, see How do I detect collision in pygame? and Making and using pygame sprites. The 'rect' and image (not img) attribute is used for collision detection. You need to update the position of the rectangle. You do not need the x and y attribute at all. Use rect.x and rect.y:
class Bee(pygame.sprite.Sprite):
    def __init__(self, x, y, alive, speed, score):
        pygame.sprite.Sprite.__init__(self)
        self.alive = alive
        self.speed = speed
        self.score = score
        self.image = beeImg
        self.rect = self.image.get_rect(center = (x, y))

    def draw(self):
        screen.blit(self.image, self.rect)
        key_input = pygame.key.get_pressed()
        if key_input[pygame.K_LEFT]:
            self.rect.x -= self.speed
        elif key_input[pygame.K_RIGHT]:
            self.rect.x += self.speed

class Honey(pygame.sprite.Sprite):
    def __init__(self, x, y,speed):
        pygame.sprite.Sprite.__init__(self)
        self.speed = speed
        self.image = honeyImg
        self.image = pygame.transform.scale(honeyImg, (60,60))
        self.rect = self.image.get_rect(center = (x, y))

    def update(self):
        screen.blit(self.image, self.rect)
        self.rect.y += 5
        if self.rect.y > screen_height:
            self.kill()

Complete example:

import pygame
from pygame.locals import *
from random import randint

class Bee(pygame.sprite.Sprite):
    def __init__(self, x, y, alive, speed, score):
        pygame.sprite.Sprite.__init__(self)
        self.alive = alive
        self.speed = speed
        self.score = score
        self.image = beeImg
        self.rect = self.image.get_rect(center = (x, y))

    def update(self):
        key_input = pygame.key.get_pressed()
        if key_input[pygame.K_LEFT]:
            self.rect.x -= self.speed
        elif key_input[pygame.K_RIGHT]:
            self.rect.x += self.speed

class Honey(pygame.sprite.Sprite):
    def __init__(self, x, y,speed):
        pygame.sprite.Sprite.__init__(self)
        self.speed = speed
        self.image = honeyImg
        self.image = pygame.transform.scale(honeyImg, (60,60))
        self.rect = self.image.get_rect(center = (x, y))

    def update(self):
        self.rect.y += 5
        if self.rect.y > screen_height:
            self.kill()

pygame.init()

#game variables
clock = pygame.time.Clock()
last_hun = pygame.time.get_ticks()
hun_frequency = 0.5 * 1000 #milliseconds
fps = 60
honeySpeed = 6
screen_width = 720
screen_height = 484

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Bee on the Run!')
bgImg = pygame.image.load("background.png")
beeImg = pygame.image.load("bee.png")
honeyImg = pygame.image.load("honey.png")

player = Bee(screen_width/2-50, screen_height/2 +120, True, 8, 0)
honeyGroup = pygame.sprite.Group()
playerGroup = pygame.sprite.Group()
playerGroup.add(player)

run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    time_now = pygame.time.get_ticks()
    if time_now - last_hun > hun_frequency:
        newHoney = Honey(randint(0, screen_width), -50, honeySpeed)
        honeyGroup.add(newHoney)
        last_hun = time_now

    #draw pics
    honeyGroup.update()
    player.update()

    hits = pygame.sprite.spritecollide(player, honeyGroup, False, pygame.sprite.collide_mask)
    if hits:
        print(f"touched")
        hits[0].kill()

    screen.blit(bgImg,(0,0))
    honeyGroup.draw(screen)
    playerGroup.draw(screen)
    pygame.display.update()

pygame.quit()


Ubuntu GUI can't take virtual keystrokes

Question: I am new to ubuntu and I am trying to recreate some projects I made in Windows and I have a problem with simulated keystrokes and simulated mouse movement and clicks.
So lets say I want to do something like pyautogui.press('a') or an alternative without using python would be to type in the terminal xdotool key a, the idea is simply to simulate the press of the letter a, and what happens is that it detects the keystroke, but depending on which program I am using it doesn't take it as an input. For example if I try to run that in the normal terminal it won't show as if I pressed the letter a, but if I run the command in visual studio or sublime text it works perfectly fine. It also seems that it has problems interacting between windows, using shortcuts and simulating mouse events.
Sincerely I just want to know if it is a feature that protects the system from unwanted simulated keystrokes or it's just something I'm doing wrong, because I couldn't find any information online about the topic.
Comment: What version of Ubuntu do you expect this to work on?
Answer: The reason it does not work is because you are using the new Wayland display protocol. Tools like xdotool and pyautogui only work in Xorg.
You can run Ubuntu on Xorg. Under Wayland, a tool ydotool that allows to simulate keystrokes in Wayland is under developement. The version in the repository of Ubuntu 22.04 is older and does not actually work. You can, however, compile a more recent version quite easily. I am currently not aware of the development or existence of Python libraries that would provide the functionality of pyautogui on Wayland.


Access parent data in nested array rendering with handlebars

Question: I have the following data structure:
A :
{
  "B": [
    {
      "C":["Hello", "World"]
    }
  ]

}
A has an attribute B which is an array. Every element of B has an attribute C which is again an array.
<template name="render">
{{#each B}}
{{#each C}}

{{this}}

{{/each}}
{{/each}}
</template>

So now I have the event handler where I want to access the element of B in which 'this' (== element of C) was rendered in. But it seems impossible. How do I do it?
Template.render.events({
    "click .clickme" : function (event, template) {
         //template.data == A
         //Template.parentData(0) == A
         //Template.parentData(1) == A
         console.log("what was my parent B?");
    }
})

Answer: You can go multiple levels upwards by using ../
In your case, suppose you want index of parent element, then:
{{@../index}}
To access another key of your parent element (not applicable in your case): {{../key}} References:
Access properties of the parent with a Handlebars 'each' loop
Handlebars.js: How to access parent index in nested each?
Edit: To access the parent element from  "click .clickme" event handler, I do not know of a direct way to access from event handler, but one possible way is to create a onclick event in html, and call a function, to which you can pass the parent element (or its index) as parameter. In the function, set is as a session variable, which can then be reffered to in the "click .clickme" event handler.
Template:
 <template name="render">
    {{#each B}}
    {{#each C}}

{{this}}

    {{/each}}
    {{/each}}
    </template>

In client side javascript, define a function:
onclickfunction = function(parentindex) {
Session.set('indexofparent',parentindex);
}

In "click .clickme" event handler, get the session variable:
Template.render.events({
    "click .clickme" : function (event, template) {
         //template.data == A
         //Template.parentData(0) == A
         //Template.parentData(1) == A
         var parentelement = Session.get('indexofparent');
         console.log(parentelement);//Shows index of B.
    }
})

P.S. Index of B is a string, use pareseInt(parentelement) to parse to integer.
Comment: thank you but I think I wasn't clear enough: I want to access the parent in the "click .clickme" event handler
Comment: I have edited the answer, please tell me if it works, also if there is slight syntax error here or there, fix it and do let me know so I can correct the same.
Comment: I did not try your new answer, because it is even hackier than what I did. You rely on that the event handler gets executed after the 'onClick' function. What I did was just setting the `data-index` of the div to the `@../index` and getting the index by `$(event.currentTarget).data('index')`. If you put this in your answer I will accept it.


T-SQL : count occurrences per unique substring in a column

Question: I want to count occurrences per unique substring in a column.
SELECT DISTINCT LEFT(code, 3)
FROM table-with-codes

Output:
code
------------------
VJCrandomthings
PASrandomthings
CAArandomthings
PASrandomthings2
PASrandomthings3

Expected output:
caa 1
pas 3
vjc 1

I tried
SELECT COUNT(DISTINCT LEFT(code, 3))
FROM table-with-codes

but that returns 3 as result.
Comment: I tried SELECT count (DISTINCT left(code, 3) ) FROM table-with-codes
Answer: You need to count with group by
select Left(code,3), Count(*)
from [table-with-codes]
group by Left(code,3)

Answer: You are correct with using left but instead of distinct you should group by
declare @table_with_codes table (code varchar(50))
insert into @table_with_codes values ('VJCrandomthings'), ('PASrandomthings'), ('CAArandomthings'), ('PASrandomthings2'), ('PASrandomthings3')

select left(code, 3) as code_3,
       count(*) as cnt
from   @table_with_codes
group by left(code, 3)

how this works
By using the group by you can count all rows that are grouped (and thus only returned once in the result), in this case all rows with the same value in lef(col, 3)
The result will be
code_3  cnt
------  ---
CAA      1
PAS      3
VJC      1

Answer: You should keep the extracted 3 letters in the select statement and Group by.
Query
Select left(code, 3) as code3, count(left(code, 3)) as cnt
From tablename
Group by left(code, 3)
Order by 1;

Answer: I don't like having LEFT(code, 3) appear more than once in the query. To improve maintainability and readability I'd refactor it using CROSS APPLY, like this :
SELECT subcode, COUNT(*)
FROM table-with-codes
CROSS APPLY (SELECT LEFT(code, 3)) AS T(subcode)
GROUP BY subcode

Answer: You can use Common Transaction Expression to avoid mentioning left function two times:
create table [table-with-codes] (code nvarchar(100))
go
insert into [table-with-codes] values('VJCrandomthings'),('PASrandomthings'),('CAArandomthings'),('PASrandomthings2'),('PASrandomthings3');

with cte as (select LEFT(code,3) mycode from [table-with-codes])
select mycode,count(1) N from cte group by mycode


CPU and fans keep working in suspend mode in Ubuntu 20 on Dell Inspirion 5530

Question: After adding an SSD to my Dell Inspirion 5530, installing Ubuntu 20 on it and booting from it, suspend doesn't seem to work anymore. Either closing the lid, or running sudo systemctl susped would result in screen and keyboard going blank, but the fans noticeably keep working and the laptop keeps being warm.
I tried the following without success:

Updated /etc/systemd/logind.conf values to suspend per this
Disabled "Wireless Radio Control", "Trusted Execution" and "Intel Software Guard Extensions" in BIOS per this
Upgraded kernel to 5.6.19 per this

Nothing helped so far. Any help would be appreciated.
P.S.
Log info:

sudo blkid: https://pastebin.com/ZqKsn152
cat /etc/fstab: https://pastebin.com/N31a3sNd
cat /etc/initramfs-tools/conf.d/resume:  No such file or directory
free -h: https://pastebin.com/iU4H5Zcw
swapon -s: https://pastebin.com/d8CpWsig
sudo dmidecode -s bios-version: 1.17.1
Comment: Did you ADD an SSD, or REPLACE an existing HDD with a SSD?
Comment: I added an SSD, and made it the bootable device (while leaving the HDD in place and reformatting it)
Comment: Edit your question and show me `sudo blkid` and `cat /etc/fstab` and `cat /etc/initramfs-tools/conf.d/resume` and `free -h` and `swapon -s`. When you're done, start comments to me with @heynnema so I get notified.
Comment: @heynnema added
Comment: Thanks. It all looks good so far. Show me `sudo dmidecode -s bios-version`.
Comment: @heynnema added
Comment: BIOS is good. What brand SSD?
Comment: SSD: Kingston 500GB
Comment: Go to the kingston web site and check for a firmware update. See https://www.kingston.com/unitedstates/us/support/technical/ssdmanager. Windows required.
Comment: Looks like I already got the latest version. Running `udisksctl  status` I get `KINGSTON SA2000M8500G, revision: S5Z42105`. https://www.kingston.com/en/support/technical/ksm-firmware-update shows `S5Z42105` to be the latest revision
Comment: Well, you've been great about responding to my questions... but I'm out of ideas... sorry. Oh wait... I just thought of one more thing...
Comment: In your BIOS, is there a setting for the disk subsystem that defines "performance" or "compatibility" or some such?
Answer: This seems to be related to a kernel version that needs updating as answered in this thread
Comment: I tried updating the kernel version to 5.6.19, unfortunately that didn't help
Answer: I would check the BIOS settings. I had such problem because in BIOS I had ACPI suspend type: suspend disabled


Regex overly splitting, how to limit split character

Question: Edited as originally posted at 2am after much frustration. Tidied up.
I'm currently trying to split a string into sections where each array entry starts and ends at == or end-of-string. String should always start with == and terminate with out any special char's at the end. Developing in Angular so code will be TS.
Example text ==key stage 4==\n===meaning===\n'''visible light''' is an [[electromagnetic wave|electromagnetic wave]] that [[human]]s can see.\n\n===about light===\n====properties====\n: '''visible light''' is a [[transverse wave|transverse wave]].\n: '''visible light''' can travel through a [[vacuum]] as well as through any [[transparent]] [[solid]], [[liquid]], [[gas]].\n: the [[speed]] of '''visible light''' through a [[vacuum]] is 300,000,000[[m/s]].\nas a [[wave]] [[light]] can be:\n*[[transmit|transmitted]]\n\n==beyond the curriculum==\n{{#ev:youtube|https://www.youtube.com/watch?v=ixxzrzxafeq}}
Needs to be split into:
1.
==key stage 4==\n===meaning===\n'''visible light''' is an [[electromagnetic wave|electromagnetic wave]] that [[human]]s can see.\n\n===about light===\n====properties====\n: '''visible light''' is a [[transverse wave|transverse wave]].\n: '''visible light''' can travel through a [[vacuum]] as well as through any [[transparent]] [[solid]], [[liquid]], [[gas]].\n: the [[speed]] of '''visible light''' through a [[vacuum]] is 300,000,000[[m/s]].\nas a [[wave]] [[light]] can be:\n*[[transmit|transmitted]]\n\n
2.
==beyond the curriculum==\n{{#ev:youtube|https://www.youtube.com/watch?v=ixxzrzxafeq}}
I've tried all manner of expressions.
Currently trying: var h2reOuter = /(?===[w+b(?<!=]*==)/gm; with little success. I've also tried var h2reOuter = /(?===[^=]*==)/gm;
Using result = str.split(h2reOuter);
But its resulting in splitting on '====' which is not valid.
Once done I will also need to search for the text inside the ==Text==. The above expression does work for this. This swaps in
#
 for ==#== and
#
 for ===#===. Got this working okay but added for context.
Any advice?
Thank you.
Comment: Are you using a programming language here?
Comment: Can you minimize the input and output text? It's pretty much unreadable. Is this JS?
Comment: Try: `var h2reOuter = /(?<===)[^=]+(?===)/gm;`  `+` requires at least one character between the equal signs.
Comment: @Poul Bak that will not solve OP's problem, will still match ===. Perhaps `(?<=^|[^=])==[\w\s]+==(?=[^=])` instead
Comment: *surely* you can create a vastly smaller test case that demonstrates the problem. Your test case is tldr material. Please aggressively edit your sample input and output down to the bare minimum, and add explanation as to the logic of exactly what constitutes the desired split locations.
Comment: @jdaz: `[\w\s]+` will not match the special characters, you must use: `[^=]+`.
Comment: Try to split using this regex: `/(?<=\\n\\n)==(?!=)/g`  - That will split on 2 *newlines* followed by 2 equal signs. This seems to be what you really want.
Comment: Use `str.split(/\n{2,}==(?!=)/)`, see https://stackoverflow.com/a/65554918/11329890
Answer: Split on == after 2 new lines and not before a =:
(?<=\n\n)==(?!=)

See live demo.
Note: If \n in your input is not a new line but a backslash then n, you'll need to escape the backslashes:
(?<=\\n\\n)==(?!=)

Answer: Use
str.split(/\n{2,}==(?!=)/)

Explanation
--------------------------------------------------------------------------------
  \n{2,}                   '\n' (newline) (at least 2 times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  ==                       '=='
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    =                        '='
--------------------------------------------------------------------------------
  )                        end of look-ahead

JavaScript code:

const str = "==key stage 4==\n===meaning===\n'''visible light''' is an [[electromagnetic wave|electromagnetic wave]] that [[human]]s can see.\n\n===about light===\n====properties====\n: '''visible light''' is a [[transverse wave|transverse wave]].\n: '''visible light''' can travel through a [[vacuum]] as well as through any [[transparent]] [[solid]], [[liquid]], [[gas]].\n: the [[speed]] of '''visible light''' through a [[vacuum]] is 300,000,000[[m/s]].\nas a [[wave]] [[light]] can be:\n*[[transmit|transmitted]]\n\n==beyond the curriculum==\n{{#ev:youtube|https://www.youtube.com/watch?v=ixxzrzxafeq}}"
console.log(str.split(/\n{2,}==(?!=)/))