Implementation of the weighted sum model without knowing the weights

Question: I am looking to combine n metrics to obtain 1 single unified metric. For example, let's say I have 2 metrics n1 and n2 for k elements. I am particularly interested in the one or two elements that have the maximum metric. It so happens that maximum of n1 gives a different set of elements than maximum of n2.
I tried to use the basic weighted sum model as
combined metric = w1*n1 + w2*n2
The challenge is that I don't know about the nature of weights ( w1>w2 or otherwise)
Is there any method to determine weights ? - all I know is n1, n2 for all k elements
Hence, I carried out 3 separate cases :
1) Assuming w1 is dominant, I randomly generated 1000 values of both w1, w2 such that w1>w2 and w1+w2=1
2) I generated 1000 values of w1,w2 assuming w2 is dominant (w2>w1 & w1+w2=1)
3) I generated w1,w2 without any condition other than w1+w2 =1
For each of the above cases, I have 1000 simulations and I get 2 elements ( from k elements )corresponding to maximum combined metric for each simulation.so I have 2000 elements for 1000 simulations. Now I check the frequency of each element and finally filter out 2 elements that have the highest frequency in the lot of 2000 elements.
I get a different set of 2 elements for the three case mentioned above, I am stuck here on how to proceed further or make sense of this
All I am trying to do is get the best out of the n metrics by combining them using a weighted sum model ( weights unknown!)
I have a few other questions in which I need help -
1) As the weights are unknown , is it justifiable to randomly create weights like I have done and combine them
2) Are there other methods available to handle this kind of a problem ?
3) Is there a probabilistic framework that assigns a probability measure to each element based on all the available metrics
All my metrics are normalized between [0,1]
Thanks.
Comment: I think you need to better define what you're trying to accomplish by combining these metrics. As you have seen, there are an infinite number of ways to combine n1 and n2 to get a unified metric. None of them are intrinsically "better" than any other. Do you want elements that do well in any score? Elements that do well in all scores? Elements that do well in one score, and not badly in the other? This needs to be defined first and foremost.
Comment: n1 and n2 are different metrics to predict failure of an element( small part in a structure). Higher the metric, higher the failure probability. It has been observed using experimental data that in some cases n1 alone gives a good prediction but n2 doesn't, in other cases n2 does best and in few cases, both don't do a good job.

Hence my idea is to combine them in order that it gives a better prediction than using a single metric.
Comment: It sounds like you may not want a weighted combination of n1 and n2, but some kind of switching logic. In most cases you should just use n1. If you know the cases where n1 is no good, switch to n2 (maybe very low n1 is associated with bad prediction, or something like that). In cases where both and n1 and n2 are bad, combining them won't help.
Answer: You're trying to get "the best" out of the n metrics. Once you define "best", you'll have your answer. If you can't precisely define "best", you won't be able to weight your metrics to achieve "the best".
In other words, you're going to need to define a target and get target data, which you can then use to optimize your weights. You probably have a vague idea of what "best" means, but as you're finding out you can't do statistics/math/optimization with ideas: you need specifics.


Why is my guitar's saddle at an angle?

Question: Most acoustic guitars I've seen, including my own, have an angled saddle (the white strip holding up the strings):

This makes the bass strings longer than the treble strings. Why is this?
Additionally, why is the second-to-highest string (B in standard tuning) offset slightly from the slant, so as to be longer than it 'should' be? I suspect this might be to do with the fact that it is four semitones above the string below it, rather than five as all the others are, but why should this make a difference?
Comment: +1 I know that thickness of string is a factor and that to give the guitar correct intonation, the saddle has to be adjusted to to compensate for this.  I don't know if this is fully related though.
Comment: its called a compensated saddle
Answer: The angle is there to improve the intonation.  If you've ever set the intonation on an electric guitar with individually-adjustable saddles, you'd see that the bass strings are slightly longer than the treble strings.  This is due to the gauge of the strings: heavier strings need to be slightly longer than lighter-gauge strings.  The B-string anomaly that you point out has to do with the fact that it and the high-E string are plain steel instead of wound, and so their intonation adjustment is different than for the wound strings.  Still, though, because the B-string is heavier than the E-string, it needs to be slightly longer.
What Does String Gauge Have To Do With Length?
When you set the intonation of a string, you want the 12th fret harmonic to match the pitch of the string when fretted at the 12th fret.  But when you fret a string, you increase both its length and its tension slightly.  Given two strings of equal length, their fretted lengths will also be equal, so since the tension of a string is proportional to the product of its mass and its length, the heavier string's tension will increase more than the lighter string when fretted.  Therefore the heavier string's pitch will increase (slightly) more than the lighter string's pitch when fretted.  To compensate for this greater increase, the heavier string needs to be slightly longer than the lighter string.
Update: I'll just point out that if you look closely at the photo, you'll see that the saddle itself is carved in such a way that the point of contact for the low-E string is towards the back of the saddle while the point of contact for the G string is in the front.  So not only is the saddle angled, but the strings contact the saddle itself at different points, increasing the differences between their lengths.
Comment: +1 Thanks, this is a good answer. But can you explain in any more detail why a heavier string needs to be longer for the intonation? (I shall not be scared by intense amounts of technical info/equations).
Comment: @Bill Cheatham: One reason the heavier strings have to be longer is the physical properties of strings in general. A perfect string has no "thickness" and will vibrate freely between two fixed points. A string in the real world has thickness, and the point at which vibration can start is offset a tiny bit from its fixed point. This is due to the material properties of the string so thickness and material composition matter. The thicker the string or stiffer the material the farther this offset is from the end point.
Comment: I'm pretty sure the explanation of why thicker strings need to be longer here is just flat out wrong.  The explanation I always saw was that the **stiffness**  of thick strings causes the *effective* witness point of the string to be in front of the *actual* witness point where it touches the saddle.  This discrepancy is greater for thicker/stiffer strings, and thus the saddles need to be farther back.  This also explains the B string anomaly, since unwound strings are stiffer.
Comment: The tension after stretching a string a certain length (e.g. fretting a string) depends on how "stretchy" a string is, for lack of a better word, and not how heavy it is.  If you wound your B string to make it as thick as your low E string you'd end up with a string that's basically the same "stretchiness", even though it has much more mass.  This winding doesn't automatically make the string's tension go up.
Comment: @Edward is correct - it's because of stiffness.  Mersenne's law (that relates pitch to string mass, tension, and length) is for an ideal string, which doesn't exist in the real world.  The stiffness of a string keeps the ends of a string from vibrating efficiently, resulting in a "speaking length" that's shorter than the actual string length.  An angled bridge or adjustable saddle pieces gets you closer to matching the speaking lengths of all the strings.  If you want to get closer still, you can use a compensated nut and fanned frets.
Answer: Another reason is the action of the string (the distance between string and fretboard). The lower strings need more space to vibrate than the higher strings and therefore they need a higher action. When you fret a string with a higher action the string tension increases more compared to a string with lower action. As far as I know this is the main reason for the different compensation lengths of the strings.
Comment: On a classical guitar, the saddle is thicker towards the thicker strings. On an acoustic guitar, the saddle has even thickness, but has the slanting. Both ways increase the length of the thick strings. Is there any reason why it's done one way or the other?
Answer: Engineer and musician's answer:
Another way you can think about it is that when you fret up the neck, you are essentially creating "fatter" strings. Imagine fretting almost all the way up to the bridge; the segment of string would start looking like a barrel. The engineering answer is that the barrel has a different area to length ratio (actually area-moment-of-inertia to length ratio).
If you can, imagine rotating a barrel end-over-end. How fast will it go? Now imagine taking all the same mass and stretching it out into a long bar. Hopefully you can see that the bar would rotate more slowly. Same mass, but different mass moments of inertia.
An idealized string is long and slender. When it starts getting short, the "barrel" nature of the string becomes more and more apparent. Vibrations of the string are a combination of 1) each string segment moving up and down, and 2) rotating back and forth. The barrel-like string (or shorter string) will rotate faster, which means a faster vibration and higher pitch.
When you fret up the neck you want a little bit of extra length to lower the pitch back down. That is what the slanted bridge does.
The b-string part on many guitar bridges is dipped down because the high e-string and the b-string are usually solid strings while the lower strings are wound. You're actually looking at two different slants because the strings are different types.
The wound strings have a lower area moment of inertia for a given diameter— there is some empty space between the different wires. This means the barrel nature of the short string takes longer to come into play as you fret up the neck. The bridge for the wound strings has a shallower slant and the bridge for the solid strings has a steeper slant.
Answer: I have another reason to consider that can be combined with the above answers.
Strings have two lengths.

Physical length,
Speaking length.

The physical length is related to the 12th root of 2. Look this up on the internet and see that it is the note spacing of Western music. The number is slightly less than 6%. Since there are 12 intervals in a scale, the frequency at the 12th fret is twice the open string frequency. Each fret is about 6% higher than the previous note and it also physically represents what a 6% compound interest means for your money by the closer fret spacing going up the neck as it relates to the time to double your money. If the 6% was not compounded all the frets would be the same space as the length of the nut to the first fret.
Speaking length has more to do about where does the vibrating string actually start vibrating. Thicker wound strings start vibrating slightly forward of their resting place at the nut, bridge and played fret. When you combine this with the above answer you will have a more comprehensive answer to the question.

Why is my guitar's saddle at an angle?


What is the difference between "onClick = { () => this.props.onClick() } and onClick = {props.onClick }?

Question: I was following the React tutorial here
Please note "onClick" attributes in the following two code snippets.
In the snipped below, an arrow function is assigned to it which calls the onClick() function passed to it in props.
  class Square extends React.Component {
  render() {
    return (
      <button
        className="square"
        onClick={() => this.props.onClick()}
      >
        {this.props.value}
      </button>
    );
  }
}

But when the class Component is converted into a functional component, the onClick attribute is assigned just the reference of the onClick function passed in props.
function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>
      {props.value}
    </button>
  );
}

Why is this so? I am confused.
Comment: Here's a good explanation of the differences.
https://stackoverflow.com/a/54057917/8628497
Answer: In class based component, the props are a property on the class instance itself. Hence you access them via this. In a functional component, the props are passed into the function as an argument.
You can read up on the basics of class based and functional components here.
Also, welcome to the site!
Answer: When you define a component using an ES6 class, a common pattern is for an event handler to be a method on the class.
function prototype approach:
function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>
      {props.value}
    </button>
  );
}

and you bind the state() in constructor using this:

constructor(props) {
    super(props);

    // This binding will help you to read "this." in the function
    this.onClick = this.onClick.bind(this);
  }

onClick() {
// u can read this here
}

Arrow function approach
one other way of doing the same things is: In the below approach you don't have to bind in constructor and use arrow function instead. inside the function, this. will have the state.
onClick = () => {
// u can read this here
}

Both are just two approaches. Arrow function approach and function prototype approach
Comment: I did not understand why you have added a constructor in the function prototype approach. There is no constructor in the original code.
Comment: Having a constructor in functional prototype approach will help you to read this.state inside your method. You can read this.props and this.state inside your functional component.


Concat data using Looker

Question: Looking to take multiple rows of data and have them all listed in one column with Looker
Basic query would look like this:
SELECT booking_id, amount, type
FROM bookings
WHERE booking_id = 'Value'

It then spits out 4 columns with each one representing a different amount based on type. What I'm wanting to do is keep the booking ID in the first column and then have the different amounts and types go into their own column on the same row.  Is this possible within Looker? Our company is making the move and most of us come from a SSMS background and are very confused.
Comment: Hmm i'm not following your question. The query you provided would return only3 columns. And also are you saying for a given `booking_id` there are multiple `amount` and `type` values? If so maybe you want to pivot on `type` so then you have one row per `booking_id` and several columns representing `type` with the values being `amount`
Answer: You can combine dimension fields in LookML using concat.  If its an integer and a string, cast the number data to a string first
dimension: combined_field_name {
    type: string
    sql: concat( CAST(${TABLE}.amount as string)), " " , ${TABLE}.type ) ;;
}

Answer: You have concat 2 ways on Looker.
once a connected data source at that time have to create a field and write a formula which I have mentioned Below

= concat(your First(1st) field name + Your second(2nd) field name)

and after that check the process if a shows true then your formula is right.
-> If you don't understand the concept then refer to below Link
https://www.youtube.com/watch?v=YDqdaPcafgw&ab_channel=UmarTazkeer
Also, do a 2nd way using your database Source.
there write a query and create a runtime field at that time you have the same formula apply

concat(your First(1st) field name + Your second(2nd) field name)

and refresh a field on Looker Studio you do both ways.
Happy Coding!!!
Comment: NOTE: This question is about Looker, not Looker Studio.
Comment: Also note that this could possibly be considered [spam](https://stackoverflow.com/help/promotion) as it's unclear why links to a specific video by a specific user and their playlist has posted in [two](https://stackoverflow.com/a/77272537) consecutive answers.

Concat data using Looker


Only allowing traffic from Cloudflare

Question: I am new to Linux System Administration and I am experimenting with iptables trying to learn how to really lock down a system with them. And one thing a friend of mine recommended was that there was a way to pass all incoming traffic through Cloudflare so even if attackers resolved the server ip they still couldn't (D)dos it directly.
This is exactly what they said:
"Simply config your servers iptables to only allow incoming connections from CloudFlares IP ranges then set it to allow only your IP/IP range to connect on port 21 (SSH)"
Could someone help me on what command I'd need to run for Ubuntu to get this effect?
Comment: SSH is port 22 :)
Comment: 21 is FTP while 22 is by default SSH http://blog.cloudflare.com/cloudflare-now-supporting-more-ports
Comment: Well yes I am aware of the ports, I've actually changed a lot of the defaults, that was posted exactly as he said it though.
Answer: --| UPDATE Feb 7, 2017 |--
This remains an advanced setup option -- as you could easily lock yourself out of your server if not done correctly. That being said, if you're comfortable doing so feel free to lock down your origin to only accept Cloudflare IPs to port 80 and port 443. Just make sure you don't accidentally block SSH.
--| OLD COMMENT from 2013 |--
We ACTIVELY discourage this kind of setup for any customer that isn't using the business level of service (at the very least). Only our business and enterprise level plans include the advanced DDoS protection option. The free and pro level plans include basic DDoS protection which mean if an attack negatively impacts other CloudFlare customer's we'll need to route that site off CloudFlare -- and then if you are only allowing traffic from CloudFlare IPs you'd then be blocking all legit traffic to your site.
p.s. I work for CloudFlare.
Comment: @mrP We don't recommend it by default. More technical users can do it though as long as they understand what they are doing (so they don't lock themselves out of their own server).
Comment: Is this still actively discouraged? https://support.cloudflare.com/hc/en-us/articles/200172906-What-should-I-do-if-I-m-expecting-a-surge-or-spike-in-traffic- doesn't indicate anything about this.
Comment: perhaps this answer should be updated to reflect the current policy? it's one of the top google results when searching for this.
Comment: @xorinzor point still remains. We don't encourage it for the average user. More advanced users can certainly do it if they'd like to however.
Answer: iptables --append INPUT --source 192.0.2.1 --protocol tcp --destination-port 22 --jump ACCEPT
iptables --append INPUT --source 203.0.113.0/24 --jump ACCEPT
iptables --policy INPUT DROP

192.0.2.1 is your IP, for SSH access. 203.0.113.0/24 is CloudFlare's IP range (if there's multiple, you'll probably have to add several rules).
Maybe you can use --in-interface to further specify which rule matches what.
Keep in mind this is a very simple example, just intended to point you in the general direction. Consult man iptables for more. Also, beware of locking yourself out.
Answer: In Addendum to Gnarfoz's reply, this is the list of IP ranges used by cloudflare:
https://www.cloudflare.com/ips
Comment: To note -- that's the wrong link.   https://www.cloudflare.com/ips  is correct.

Only allowing traffic from Cloudflare


Every time I press 'record' I forget how to play and sing!

Question: I have a very simple home setup with a 4-channel USB interface, into which I plug my electro-acoustic guitar and a decent mic (SM58). Purely for fun/learning/curiosity what I sound like, I will often try recording myself playing and singing a song I know well.
I'll practice without the mic, and I'll even practice with the mic but with Audacity not recording. And yet unfailingly, the moment I press 'record' on Audactity, it all goes horribly wrong. My voice totally screws up (like one of the blooper interviews on X-Factor) and I will forget a 4-chord sequence that I can play perfectly.
I guess this is a perfect example of performance anxiety, would you agree? I don't actually feel anxious because nobody is listening and I can delete the recording.
What can I do? Practice doesn't seem to be helping. Just pressing record and trying to forget "I'll edit out the good take" doesn't seem to help either, the simple fact I'm being recorded ruins my performance.
If I record using my phone, the problems doesn't really happen but the quality isn't good enough and anyway it's hardly a solution :)
Comment: Leave your equipment set up. Leave the record button on even when warming up. Always warm up playing loads of songs you like. Some days are not meant to be so don't expect a good session every time. Don't delete stuff unless it really is naff. Wait a day or two before listening back with a critical mind.
Comment: Either play the first version to a loud click-track & consider it a guide, or put guit & vox down separately, guide or not, & go back through dropping in to fix the bits you didn't like. A 'record' doesn't have to be a single live performance; there's a certain aspect of 'playing the studio as another instrument' that can be done to make the parts a coherent whole.
Comment: I can relate.  Sometimes I make videos of myself performing a song (guitar and vocal) for YouTube.  And I have the same experience when the red record light on the camcorder is on. It's less nerve racking to play for an audience live than to play for the camcorder.  I find that if I am learning to cover a new song and I want to be sure I have it what I call "ready for prime time" or "stage ready", I will record myself performing the song.  By the time I get a take that I can live with - I have mastered the song.  And I know if I can perform it for the camera, performing it live will be easy.
Comment: Very similar to this question: http://music.stackexchange.com/questions/29318/i-want-to-record-but-am-nervous-about-it-why-how-to-get-ready
Comment: Yeah maybe I'll just hit record before I even get tuned, and try to ignore it... although the microphone in my face is a bit scary too :)
Comment: Good points but I specifically want to practice doing it live - and also I find it weirder singing without an instrument to play ;)
Answer: Well, ok, I see what you're saying.  Keep that record button down all the time then.  If that makes you suck all the time, then so be it.  Suck and slowly improve.
If your music is only being listened to by you, how do you really know it's any good to begin with?  You can't judge yourself while practicing.  That's a skewed view.  You have to judge yourself on a recording or, you know, it really doesn't count.
How good you "can possibly if you're lucky" play is usually way different than how good you "do actually reliably, repeatedly play on an average take".
Keep the record button down.  Practice with the record button down.
Comment: `If your music is only being listened to by you, how do you really know it's any good to begin with?` Are you saying I don't really sound that good while singing in the shower?
Comment: I agree with @Stephen; if you suck when you hit record, that means that you're too comfortable where you are, and hitting record is taking you out of your comfort zone. So as Stephen suggested, you should keep record on all the time so you can practice and improve. You can't overcome a fear by avoiding it.
Comment: Playing the wrong chord for the 14th time is "learning your mistakes". Simple enough to play from a cheat-sheet when recording. You don't have to fully learn it until you go play it live. Same goes for instruments & vocals. Usually, when I'm recording, I'm still working out the parts as I go, so nothing is cast in stone. Sometimes I put all the choruses down, then all the intros, then find something for the verses etc - the full piece is never played as such until long after the record is out, & partly, I guess, I learn the piece then by having listened to the record so many times.
Comment: `You can't judge yourself while practicing. That's a skewed view.` that's a nice point of view you have there, I'll keep that in mind when recording vocals for the next time!
Comment: While I can't play back something I don't record, I can notice when I totally miss the note I'm aiming for and play the wrong chord for the 14th time in a row (it reminds me of the recording scene of "If you love Christmas" from Love Actually)
Answer: Notwithstanding the other answers, which as of this date, are all sound advice [no pun intended], so I shan't repeat anything already mentioned.
First of all… Red-light fever is a real, but purely psychological, effect.
The only way to get over it is to get over it.
Some things that may help… or not...

Take off the headphones.
If you're monitoring the repro with added reverb/sweetness etc only when recording, remove that difference.
Learn to live with it.
If you only think you sound bad when recording, you have the wrong 'head' on. You feel have to get it right every single time - but no-one ever does. There's always a better take. Ask Sinatra, Hendrix, McCartney etc etc etc. I guarantee they thought there was a better one available, until the producer told them they had it already.
[Example: Cilla Black recording "Alfie", way back in the 60's… after something like 20 takes, George Martin nudges Burt Bacharach & hints that they may actually have had it in the can at about take 5. [can't find a citation, but wikipedia hints at it.]
Have the fear hammered out of you!
There's nothing like being on someone else's time when the red light is on.
My first ever professional lead vox session was as a clueless newbie who was fine singing on a pub stage with my unknown band or in my own home studio - the transition to 5 guys I'd never met before, but whose names/reputations/images/previous recordings I knew already, watching me through the double-glazing in a real studio costing who-knows-what bucks an hour, was the scariest thing I ever did. I took 12 hours to get the lead vox down, after which I could barely see, let alone speak.
It taught me the basics of everything I now know.
If that scenario feels like a true impossibility, get a friend to record you & be heartlessly [if professionally] critical while you do it.
Comment: I first heard it back in the 80s - the term came from the studio engineers & producers of the time. idk how far back it goes before that. It may be a UK-only term, idk that either, sorry.
Comment: Is **red light fever** a common term, or something you made up?
Answer: I have/had (no clue if it still happens) The same issue, it's all about your subconcious need to do it perfectly. When you don't care and just do it you just do it, as soon as there's a hint that you'r monitored ... boom. My advice is just keep recording yourself and it'll wear out with time. I have the impression that that's what did it for me.
I guess it goes like this: When recording becomes a routine, it's no longer something to worry about...
On the other hand, do you realise that you "suck" while you record or do you have to listen to the recording and then judge it? E.g I had timing issues, but I was hearving everything all right while I played, it was because of latency. Or sometimes I actually sucked and didn't notice until I heard again the recording.
Practice Practice Practice, Practice EVERYTHING and then practice EVERYTHING SOME MORE!
Answer: I know you said that you do most of this, but I will say it anyway. The key is making it routine and even boring. Do it (record yourself) every day until you forget what the problem is you are trying to solve. Make it part of your practice. Record things you do not plan to record or listen to again ever. Don't listen to them for a couple weeks, at first. The experience of listening to your recordings affects and participates in this problem too. You will not hear your self in the right light until you forget the experience of the recording session. Save that for later when you find your self getting more comfortable.
No sense in not pressing the recording button, or pressing the pause button. You do not experience the problem then. Nothing is solved from the placebo because you know it is truly a placebo.
I assume that you are recording to hard drive with lots of space. Try starting the recording, tell yourself your are either going to take several live practice takes, or that you are simply going to practice with the recorder on. You can change your mind if something turns out... but plan to delete it for the first week or two. Practice recording everything you play. Practice and play the piece until you forget you are recording. and repeat that practice every day for a few weeks. Practice doing this, as part of your practice for a week. Eventually your fear will succumb to boredom if nothing else, letting your talent into the recording with relatively little self consienceness. Plan to dump the recordings. Throw them out. You can change your mind when you get something good.
If you are willing to have an accomplice, and experiment you can try is this:
Have a partner decide to record or not record a performance at random in a way so that you do not know from moment to the next if you are being recorded or not. This ignorance of the recording process could help your initial work/practice to overcome your habit/difficulty/fear.
An Important Note: Most people do not like the sound of their own voice, and sometimes that extends to playing a non vocal instrument too. I have felt that way about both, but over time learned to hear by instrumental playing with a kinder critical eye/ear than I do with my voice. Do not be discouraged by this. It will take time to learn to hear the sound of your own voice without feeling and focusing on the fact that it does not sound like you, does not sound right too you (the way it was in your head when you hear your voice live). It will also take time to stop hearing the slightest nuances of your performance that weren't quite exactly what you wanted as mistakes. Call them happy accidents. Things that happen that were new and different than planned, but perhaps valid. Practice being a kinder judge to your own work, as you are likely to be too critical and not a worthy judge. After decades, I still am too fussy about my own work and totally clueless as to how good it is. I understand Hendrix hated his own voice and was always asking/ telling the engineers to turn it down and turn up the guitar (citation needed (I read too much Wikipedia)). This is why you should not listen to the recordings until after you start to get more comfortable with the process.
Digital effects in play back can help but be leery, it is common for people to want to engineer (mixing, recording, adding effects etc.) their own voice poorly even if they are good at engineering the rest of the time. There is a tendency for most to want to hide their voice in effects. However it can help your recording to listen to the monitor (on head phones) your live recording as it happens if you put some effects on the playback, so consider experimenting with that. There is more than one pitfall there. One such problem is that you can be distracted by your own recording sound and effects, much like a bad cell call where your words echo back to you as you speak.
Answer: Personally, the record button always made me play better. As I was never trained or took lessons, it would keep me from trying to do fancy stuff and just do it right, the easy way.
You don't feel it when you use your phone because you would (hopefully) never make someone else suffer through that horrible sound quality, nor will it ever be a 'release', but anything recorded in Audactity can be turned into a masterpiece with the right know how.
So if you're thinking, if you could manage to do it right, just this once and get the track laid down... stop right there.
Trying to lay down a track focused on a single song isn't easy and to me it's very annoying to play the same song over and over. If we were trying to make a demo, we'd keep running through the thee or four songs we chose. Recordings of our regular practices would run over an hour long and you'd start to forget about it after the about the third song. Turing the monitor off helped; put some tape over that red light. Having an accompaniment makes everything easier. You'll (should) be more concerned with the other humans in the room than the CPU that's recording you.
At a quick estimate, I've recorded over 1500, hour-long sets. Out of all of that there's about three sets that contain a single song that I would even think about making someone else listen to. None of them are on any of our demo takes either; apparently we had the same problem.
To paraphrase Ronald Speirs:
It's going to be crap. You are going to make mistakes. Now get back to work.

You are experiencing anxiety.

Performance Anxiety is what happens when you focus on yourself and your anxiety, rather than your presentation or performance. It stems from a tendency to resist and fight your anxiety, rather than to accept and work with it. It's the result of thinking of the performance situation as a threat, rather than a challenge. -anxietycoach.com

"Farthest from your mind is the thought of falling back, in fact, it isn't there at all. And so you dig your hole carefully and deep, and wait." -- Scrapbook of the 506th PIR

Craft-> Confidence-> Courage-> Competent musician
Answer: Do you have the music/lyrics in front of you in a convenient format?  If you are trying to perform it all from memory, I think that needlessly gives additional difficulty to what you are trying to do.
As a non-musical, real-life analogy, many years ago when I first needed to leave voicemails I would run into similar performance anxiety.  I dealt with it my writing down what I wanted to say in the voicemail first, and then the process became much easier.  With time and practice, recording voicemails became much less intimidating, though if it is important, I still sometimes write a couple of notes before recording the voicemail.

Another thing you could try - Invest in a cheap dictaphone (or voice memo phone app) and use it to dictate what you are doing whenever you do a no-pressure activity (e.g. washing dishes, folding laundry).  Spare no details in your dictation.  You might want to start off not listening to the results, but as you get more comfortable you can listen to some or all of the recording  to get more used to the sound of your voice recorded.
Everyone likes to sing in the shower.  Perhaps set your recording device going and record your entire shower session :).
Answer: Every time I press 'record' I forget how to play and sing!
Well, the solution is obvious: don't press "Record".  Get enough media that you can record the whole day long routinely.
Don't stop in a bad recording.  Don't stop after a good recording.  It doesn't matter.  If there is a bad recording, there will be a better one.  But there is no point in stopping your practice routine if the results are inconsistent yet.  If there is a good recording, there will be a better one.
You are inconveniencing noone but yourself.  Make sure that your recording equipment is in a state where you are not wasting any of your own time.  If you feel you had a really good recording, don't interrupt.  Just write down the wall clock time and continue.
At the end of the day, you can listen in to those particular recordings and see what is worth improving.  Work on that.  If you wrote nothing down, at least check a few spots to see whether gain and lighting are fine: it's a nuisance if you get a perfect performance and messed up the recording.
Things are different when somebody else is recording: then you want to work focused and with a somewhat finite time span until completion.  But as long as you are the one dealing with the recording equipment, just make sure that you have what it takes to keep recording routinely without a hassle.
Answer: If all fails, maybe you could get a friend to sneak up on you, press the record button, and record you while you think your practicing. I'm not sure if you can see them press it, but it could help if you don't press the record button.


Unhandled Exception: Android.Content.Res.Resources+NotFoundException:  occurred

Question: I tried picking a picture from my gallery from an app I developed using Xamarin-forms, visual studio 2017. After selecting a picture, i keep getting that error. same as when using camera to take the picture.
I tried debugging it. But i was not able to get anything concrete from it.
It crashed when it got to var mediaFile
<code>using Plugin.Media;
using Plugin.Media.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App11.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Levels : ContentPage
    {
        public Levels ()
        {
            InitializeComponent ();
        }

        private async void TakePhoto_Clicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                await DisplayAlert("camera", "not supported", "OK");
                return;
            }

            var mediaFile= await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
            {
                Directory = "Sample",
                Name = "test.jpg"
            });

            if (file == null)
                return;

            await DisplayAlert("File Location", file.Path, "OK");

            image.Source = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();
                return stream;
            });

        }

        private async void FromPhone_Clicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsPickPhotoSupported)
            {
                await DisplayAlert("No pickphoto", "Not available", "OK");
                return;
            }

            var mediaFile = await CrossMedia.Current.PickPhotoAsync();

            if (mediaFile == null)
                return;

            image.Source = ImageSource.FromStream(() =>
            {
                return mediaFile.GetStream();
            });
        }
    }
}
</code>
Answer: Please take the following code about taking photos:
<code> private async void Takephoto_Clicked(object sender, EventArgs e)
    {
        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            DisplayAlert("No Camera", ":( No camera available.", "OK");
            return;
        }

        var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            Directory = "Test",
            SaveToAlbum = true,
            CompressionQuality = 75,
            CustomPhotoSize = 50,
            PhotoSize = PhotoSize.MaxWidthHeight,
            MaxWidthHeight = 2000,
            DefaultCamera = CameraDevice.Front
        });

        if (file == null)
            return;

        DisplayAlert("File Location", file.Path, "OK");

        image.Source = ImageSource.FromStream(() =>
        {
            var stream = file.GetStream();
            file.Dispose();
            return stream;
        });
    }
</code>
Here is the sample that you can take a look:
https://github.com/jamesmontemagno/MediaPlugin


File Pointer locations in a text file in C

Question: I am trying to find how the file pointer moves while traversing the file.
For that purpose, I have written this piece of code -
#include<stdio.h>
#include<conio.h>
void main()
{
    FILE *fp;
    fp=fopen("example.txt","w+");
    fputs("This is a test",fp);
    printf("The initial text - \n");
    int x=0;                                                     // For the purpose of debugging
    rewind(fp);
    while(!feof(fp))
    {
        char ch=getc(fp);
        printf("File pointer  - %d and letter - ",ftell(fp));
        if(ch=='\t')
            puts("tab");
        else
        if(ch=='newline')
            puts("\n");
        else
            putchar(ch);
        printf("\n");
    }
    fputs("\nThis is the second line",fp);
    printf("\n\nThe final text - \n");
    rewind(fp);
    while(!feof(fp))
    {
        char ch=getc(fp);
        printf("File pointer  - %d and letter - ",ftell(fp));
        if(ch=='\t')
            puts("tab");
        else
        if(ch=='\n')
            puts("newline");
        else
            putchar(ch);
        printf("\n");
    }
}

Now, the O/P for this is understandable except for 3 places -

When the first line is inputted, why is the pointer value for the 14th position present twice?
Isn't the file supposed to end at the first occurrence of the EOF - 14.
Why does this happen?
After the second line is inputted, why is the 15th position of the pointer missing?
Why is there a line empty after the 16th character?
Isn't the 17th character supposed to occur on the next line itself without an empty line?
Comment: Possibly related: ['Why is "while ( !feof (file) )" always wrong?'](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong)
Comment: Just as a friendly reminder: `int fgets(FILE *stream)` <= There's a reason for the `int`, here.
Comment: Also, posting the output you're referring to wouldn't hurt.
Comment: Please don't edit the code in your question - it causes confusion and invalidates comments/answers. Also, copy/paste in the source you built and tested, ie. source that compiles.  Posting code that does not compile gives the impression that you have not actually built and tested it at all and have just transcribed it from printed homework:(   If you want to show changes that you made to assist with debugging, APPEND the new code underneath, with suitable comments, or ask another question.
Comment: Your compiler should be spitting out warnings, don't ignore them.
Answer: You want this:
#include<stdio.h>
#include<conio.h>

void main()
{
    FILE *fp;
    fp=fopen("example.txt","w+");
    fputs("This is a test",fp);
    int x=0;                                     // For the purpose of debugging
    rewind(fp);
    while(1)                  // changement here
    {
        int ch=getc(fp);      // changement here
        if (ch == EOF)        // changement here
          break;
        printf("File pointer  - %d and letter - ",ftell(fp));
        if(ch=='\t')
            puts("tab");
        else
        if(ch=='\n')
            puts("newline");
        else
            putchar(ch);
        printf("\n");
    }
    fputs("\nThis is the second line",fp);
    printf("\n\nThe final text - \n");
    rewind(fp);
    while(1)
    {
        int ch=getc(fp);   // changement here
        if (ch == EOF)     // changement here
          break;

        printf("File pointer  - %d and letter - ",ftell(fp));
        if(ch=='\t')
            puts("tab");
        else
        if(ch=='\n')
            puts("newline");
        else
            putchar(ch);
        printf("\n");
    }
}

Now the output should what you expect.
Your problem was following: you read a charactewr with getc, but you dont test if it's an EOF (end of file). The while (feof(...)) is useless, because once you have read the last char of the file, feof still is false, therefore you enter the loop again, and this time getc returns EOF which you ignore.
See also this question.
BTW

I'm not sure what output you actually expect.
the second while loop is identical to the first one, you should put that in a function.
Comment: ftell returns a long. %d might print nonesense.
Comment: @user1320881 true, but most likely not relevant here as `long` is most of the time the same as `int`.
Answer: There are some stuff that you had to know before you even used them.
#include <stdio.h>
//#include <conio.h>
void main()
{
    FILE *fp;
    fp=fopen("example.txt","w+");
    fputs("This is a test",fp);
    printf("The initial text - \n");
    int x=0;                                                     // For the purpose of debugging
    rewind(fp);
    while(!feof(fp))
    {
        char ch=getc(fp);
        if(ch==EOF){
            break;
        }
        printf("File pointer  - %ld and letter - ",ftell(fp));
        if(ch==EOF){
            break;}
        if(ch=='\t')
            puts("tab");
        else
        if(ch=='\n')
            puts("newline");
        else
            printf("%c",ch);
        printf("\n");
    }
    fputs("\nThis is the second line",fp);
    printf("\n\nThe final text - \n");
    rewind(fp);
    while(!feof(fp))
    {
        int ch=getc(fp);
        if(ch==EOF){
            break;
        }
        printf("File pointer  - %ld and letter - ",ftell(fp));
        if(ch=='\t')
            puts("tab");
        else

        if(ch=='\n')
            //printf("newline");
            //fputs(input, stdout);
            fputs("newline",stdout);
        else
            putc(ch,stdout);
        printf("\n");
    }
}

You need to check whether the character is actually an EOF
In a file even EOF is considered as a character to represent the end. the feof function assumes that also to be a character and hence passes that loop. there you get a junk
puts actually inserts a newline at the end when you use it. To avoid it use fputs instead
First point should explain it.
Cheers.


Windows Remote Desktop Cluster - can we force some users to a specific server?

Question: Given an RDS cluster with NLB, users connect and login to one of the servers at random.
But there is one program that's only available on one of the servers (licensing reasons).
Can we force a group of users to always be logged on to one specific server, and let everyone else have logins allocated normally?
Answer: Not with NLB. If you need them to log onto a specific RDS server you'll need to have them connect to the ip address or name of the specific server.
Comment: But if you do that, it appears that NLB / terminal server session broker can still make the connection actually to a different server.
Comment: NLB won't do that. You didn't mention that you were using RDS Session Broker. You could use a RemoteApp to direct these users to the server where the application is installed.
Comment: I can't see how that would work. If you have an open session on the 'right' server then you don't want to launch a remoteapp, and if you have a session on a 'wrong' server, the remoteapp login will hit the session broker and ... what, clash or takeover your current login?
Comment: The point would be to launch the RemoteApp, not to log in to a session and then launch the RemoteApp. You could put a shortcut to the RemoteApp on their desktop, which they would launch. Then if they need to they could login to an RDS session and launch whatever other applications they need to use. The point being that when they need to run this program they only run it from the RemoteApp, regardless of whether they have a session on the right or wrong server. The other option would be to create a separate RDS farm for this server and have these users connect to this new farm.


Is the convention for reporting football results universal?

Question: In Britain (and throughout Europe and the football-playing world), it is the normal convention to state the name of the home team first and the away team second. So when the results are read out on the radio the announcer will say, for example:
"Queens Park Rangers 2 Sheffield Wednesday 3".
(The match was played at the ground of QPR)
However I have been informed that such a convention is foreign to Americans. In the USA, the score of the winning side is always read out first.
Is this the case or not?
Comment: Also: https://sports.stackexchange.com/questions/17589/which-team-is-mentioned-first-in-the-score-in-nba-games
Answer: Yes, the convention in the US is to report the winning team first.
Take Wednesday's article in the New York Times on Tuesdays's Yankee-White Sox. matchup played at Guaranteed Rate Field (the home field of the White Sox).
The header reads "YANKEES 7, WHITE SOX 3," although the score isn't mentioned directly in the text.
Likewise, Wednesday's article on the Reds-Mets game (at the Mets' home field), lists the Reds first: "REDS 3, METS 1, and the copy matches.

Sal Romano got the win in his New York homecoming, pitching into the seventh inning to lift the Cincinnati Reds over the Mets, 6-1, on Tuesday night in a game delayed by rain for 1 hour 40 minutes in the first inning. [emphasis added].

I added two examples from New York home teams to show it isn't a convention to list the local team first either. I would also note that in baseball, the away team always goes first and the home team second, so it would be very normal to always list [away]-[home], although that isn't the case (see today's article on the Yankee's home win, where the Yankees are listed first).
For a soccer example, I dug up this 2015 article on the New York City F.C. vs. Portland Timbers game, which was played on NYCFC's home turf.
In this article, the Timbers (away) are listed first in the header: "TIMBERS 1, NEW YORK CITY F.C. 0 and in the copy:

Dairon Asprilla scored a late second-half goal Sunday night, and the Portland Timbers beat New York City F.C., 1-0.

Anecdotally, growing up in America, I was always taught to list the winning score first, regardless of sport. In fact, I wasn't even aware anyone did it differently.
Comment: +1 and a tick for a comprehensive answer. It is exactly the information I was seeking. But what do you do if the match ends in a draw?
Comment: However. When showing the score *during* a game (at least on TV), the away team is listed first and the home team is listed second.
Comment: @JasonBassford In soccer or baseball? I know it to be true for baseball and (American) football, but I don't think I've ever watched a soccer game.
Comment: Honestly, I'm not certain about North American soccer. But if [this Reddit thread](https://www.reddit.com/r/MLS/comments/2aophn/why_is_the_home_team_listed_first/) is any indication, it does specifically follow the European format in the reverse of the other North American sports. (At least in general. Apparently, some networks aren't consistent.)
Comment: What about tennis, where they typically report the scores of all the sets, e.g. *6-4, 2-6, 6-3*. Is the match winner first? Does this differ between US and Europe?
Comment: @Barmar The match winner is shown first. (Although it could be winner 4-6, 6-2, 6-3.) But during the match, where they show one player name above the other, the name on top is normally the player seeded (ranked) highest. I've noticed no difference between any of the tournaments in any of the countries.
Comment: @JasonBassford Well, during an individual game, it is the server's score that is first.  But tennis the soring system is rather different, as it is in sports like golf, cricket etc. It would make no sense whatever to follow the football pattern.
Comment: @ws2 In tennis, there are two different scores. One is for the match itself (which I assumed was the most relevant), and the other is for a specific game. And in the event of a tie (not in tennis, of course), I'm guessing that the same order is used as during a game, not afterwards when a winner is known.
Comment: @WS2 most American sports don't conventionally end in a draw. I'm not sure in that case, I suspect it would go away-home, following the baseball convention
Comment: Football (the American sport) and soccer have become national and international sports with improved communications.  There's really no home team at the World Cup or the SuperBowl.
Comment: @Xanne But as far as the World Cup is concerned (also any domestic matches in Britain, played on a neutral ground, such as the FA Cup)  the order in which the teams names are read out is fixed at the time of the draw. And it stays the same irrespective of who wins, or whether the match ends in a draw.


What is the duration of chords shown on top of a measure

Question: In the image, there is an A Major over the first measure. Should it be also played over the 2nd, 3rd and 4th measures even though the chord is not written on top? Until the D chord in the 5th measure?
Answer: Yes, the A chord applies to all of the measures until the next chord indication. That chord in turn (the D) lasts until another different chord is given.
Answer: There's no point in repeating the chord symbol if the chord remains the same. Only when it changes is it necessary to indicate the new chord. Which won't have its symbol repeated, but that will continue, bar by bar, until a different chord is needed. Hence the next A, after which it keeps getting played until...the next change. (Which I'm guessing will be E).
Comment: It is E indeed!
Comment: An excellent deduction Mr. Holmes!
Comment: @JohnBelzaguy - I do me best. Sometimes it works..!


Como evitar que a linha perca a referência da cor no ExpandableListView?

Question: Tenho um ExpandableListView com 5 grupos e cada grupo tem seus filhos(Child). Eu mudei a cor de fundo de alguns filhos, porém quando eu uso o scroll bagunça todas as linhas, pintando tudo. Isso acontece por causa do recycle do listView, quando estou usando um listView simples eu sobrescrevo os métodos getViewType e getViewTypeCount e resolve meu problema. No expandable possuem os métodos getChildType e getChildTypeCount, porém não está funcionando. Alguém já trabalhou com estes dois métodos no expandable e pode dar uma ajuda? Agradeço desde já.
Answer: Fala Daniel,
Coloca seu código aqui, é melhor pra te ajudar.
Mas provavelmente seu problema é que você faz um IF para colorir as linhas né?
Então você precisa colocar um ELSE pra cor padrão, exemplo:
if(filho == filho){
   //cor da linha muda
}else{
   //cor da linha padrão
}

Isso acontece porque você está usando RecyclerView e como o nome diz, ele recicla as células já usadas pra criar uma nova.
Abraços.
Comment: eu já tinha tido esse problema com listView há muito atrás e essa solução não tinha funcionado, mas com o expandable deu certo. Obrigadão. Abraços.


CSS positioning absolute vs. relative

Question: I have an  tag in my HTML that I need a drop-down to always be displayed below when the user clicks on the link. Without the click event, it looks something like this:
HTML
Learn more

This is the stuff for learning more

CSS
.parent {
  position: relative;
}
.child {
  position: absolute;
}

However, the child div is not lining up with the  tag if there is content before the a tag.
How can I ensure the dropdown is always appearing below the  tag even if there's content before it?
Comment: You can use  instead of using
Answer: Not with the code you have there.
You have missed something when it comes to relative/absolute positioning. For the absolutely positioned element to align with the relatively positioned element, the relative element needs to be a parent. That's not a parent, it's a sibling, and won't work.
To fix this, you'll have to rethink it somehow. You could use javascript to always align the element with the link, or you can wrap the whole bit in something like another div, akin to this:

  Learn more

This is the stuff for learning more

and CSS something like this:
.parent {
  position: relative;
}
.child {
  position: absolute;
  margin-top: 1.5em; // to clear the
}

CSS positioning absolute vs. relative


How would one graph the results of subjective rank order?

Question: I'm looking for way to visualize subjective rankings, separate from my non-parametric tests.
I've asked 12 participants to rank 8 different items according to different subjective criterion (separate rankings for each one). For any individual set of rankings, I'm looking for a good way to visualized the high-level trends of the rankings.
I've tried both bar and radar plots on the average rankings, and I've seen one other person use a scatter/balloon plot over the number of responses per ranking, but I'm not quite sure what conveys the best overview. Either I can use the 8 mean rankings, or the the 8 counts of each ranking per item.
Edit:
For example:
Each column is an item, each row is a person's ranking of each of the eight items. Not a particularly strong agreement in this example, but in general would like to understand the best way to convey the overall trends.
<code>                        Item:
            A   B   C   D   E   F   G   H
Rater:
  1         6   8   1   7   3   4   2   5
  2         1   3   8   7   6   5   2   4
  3         5   8   7   6   1   4   2   3
  4         5   8   7   6   4   2   1   3
  5         1   2   8   7   4   3   5   6
  6         1   7   8   5   6   2   4   3
  7         5   1   8   4   7   3   6   2
  8         4   2   8   7   6   1   5   2
  9         6   3   8   4   7   1   5   2
  10        3   2   8   7   4   1   5   6
  11        2   3   7   8   1   5   4   5
  12        8   5   6   7   2   3   1   4
</code>
Comment: I was asking specifically about the item ranks, but if there's a good way to show inter-rater agreement other than something than Kendall's W, all the better too.
Comment: It might help if you could list some of your data. Also, what software are you likely to use?
Comment: I've added an example matrix, and I'll likely be plotting in Excel.
Comment: Are you trying to display the item ranks or the inter-rater agreement (or both)?
Comment: You could plot the empirical CDF of each column. This would look something like a (whole mess of) ROC curves. Not sure what would jump out if you did this, though.
Answer: I've done something similar for visualizing similar rankings.  The method I used gave me a quick snapshot of how the rankings related-nothing more.  My solution used Excel 2010 sparklines to create a small-multiples view of the rankings (this can be done in other Excel versions, but it takes a bit more work).  Also, I generally use Excel's Table functionality just to speed things along.

Transpose your matrix so the rows hold all the values per rated item.
If rank 1 is best, invert your ranks so 1=8,2=7... this just helps you visualize the chart's columns (rather than blank space) as better.
Sum the results of each question's raters.  Then sort by this total.  This will put the item with the best overall scores first and worst overall scores last.  This ranking will help you visually rank the charts when the overall pattern may not be obvious (as was the case in your sample data.
Insert Sparklines using your ranking data (without the totals column) next to your data table.  Visually, the more/bigger bars there are, the better the overall ranking.

There's nothing particularly analytical about this approach, but it's a pretty quick way to visualize the data.

Note, if you don't have Excel 2010, you can create stripped down, cell sized column charts for each row that look about the same.  Or, you can use a third-party add-on to create them.
EDIT:  Table and Chart utilizing Gung's suggestion for average measure.  As pointed out, since the scale is similar, it was added to the chart as an additional point of comparison (I used gray to help differeniate it from the raw data plots).
Comment: +1, this is a really nice, simple solution for this situation. One small suggestion I would make is to use *averages* instead of sums in step #3. This won't really change anything, but puts the total back into the original scale, which can make the interpretation of the value easier.
Comment: @gung, thanks for the feedback.  I think using the averages really works well (as above).  I never liked using the totals, but hadn't put too much thought into a good alternative.
Answer: I would do this in the form of a bumps chart. It looks pretty cluttered, so I would work with the colors to try to help this out.
The sparklines @dav suggested don't really allow a comparison between products.
Comment: I think this chart would be excellent if the dot sizes were scaled in reverse order from 8 (smallest) to 1 (largest). I think the trends might jump out more that way.
Answer: I know this question is seven years old and see the poster is AWOL, but I do appreciate a question that includes data...
It sounds like product rating trends are the issue rather trends of the individual raters. In which case, you can get a sense of the high-level trends by plotting the means and confidence interval for each product.

At least we can see that C and D seem worse than the others (assuming 8 is the worst ranking). Maybe A, B and C have more disagreement than the others.
If individual raters are important in convey, you could use different marks and colors for each rater's dots.
Answer: Dot plots can be very useful for this type of analysis.
This is what I came up with your data. You can also add connecting lines.

I would also think about adding a more prominent series to show the average for each option.
Some links:
http://peltiertech.com/Excel/Charts/DotPlot.html
http://peltiertech.com/WordPress/charting-survey-results/


Are neighborhood bases always a subset of a basis?

Question: The definition of basis and neighborhood basis are:
Let $(X,\tau)$ be a topological space, a base of $\tau$ is a subset $\mathfrak{B}$ of $\tau$ such that each open set $A \in \tau$ is union of elements of $\mathfrak{B}$
If $p \in X$, a subset $\mathfrak{B}_p\subseteq U_p=\{U \in \tau | p \in U\}$ of neighborboods of $  p$ is called a neighborhood basis of $ p$ if for each $U \in U_p$ there exist an $V\in \mathfrak{B}_p$ such that $V\subseteq U$
Additionaly, I have the following proposition in my lectures notes
Let $(X,\tau)$ be a topological space
(1) If $\mathfrak{B}$ is a basis of $\tau$ and if $p\in X$, the set $\mathfrak{B}_p=\{B \in \mathfrak{B}| p \in B\}$ is a neighborhood basis at $p$.
(2) If for each  $p\in X$ a neighborhood basis at $p $ : $\mathfrak{B}_p$ is defined, the set $\mathfrak{B}=\{B \in \mathfrak{B}_p | p \in X\}$ is a basis of $\tau$
and the definition of countable basis and countable neighborhood basis
Let $(X,\tau)$ be a topological space. If $\tau$ admits a basis whose cardinality is countable, $\tau$ is said to have a countable basis
If  each $p \in X$ admits a countably-infinite neighborhood basis, $\tau$ is said to have a countably-infinite neighborhood basis
In those cases $(X, \tau)$ is called a  countable-basis topological space or countable-neighborhood-basis topological space respectively.
*The term countable includes finite sets: a set is countable if it is finite or countably-infinite.
Then they say that by the second statement (2) of  the initial proposition, countable basis implies countable neighborhood basis(that is if $\mathfrak{B}$is countable, then $\mathfrak{B}_p$ is countable), but the converse is not true. ....(3)
A couple of related questions

Are neighborhood bases always a subset of a basis as stated in (1) : $\mathfrak{B}_p=\{B \in \mathfrak{B}| p \in B\}$ ?, or this is just one possibility?
I attempt to give an explanation of (3):

Since by (1)(they used (2), but I do not agree, I Think I need (1)), $\mathfrak{B}_p \subseteq \mathfrak{B}$, $\mathfrak{B}$ is countable by hypothesis  and a subset of a countable set is countable, it follows that  $\mathfrak{B}_p $ is countable.
The converse is not true, because we could have an uncountable number of points in X, for which the union of countable neighbordhood bases might not be countable
Is this explanation correct?, as you see it depends heavily on the definition of  $\mathfrak{B}_p$ as a subset of $\mathfrak{B}$, so it wouldn't be general if a neighborhood basis could be defined without taking the elements from a basis.
Answer: If $\mathscr{B}$ is a base for a topology $\tau$ on a set $X$, and $p\in X$, the set $\mathscr{B}_p=\{B\in\mathscr{B}:p\in B\}$ is always a nbhd base at $p$. However, it is entirely possible to have a base $\mathscr{B}$ for $\tau$ and a nbhd base $\mathscr{B}_p$ at $p$ such that $\mathscr{B}_p\cap\mathscr{B}=\varnothing$. For instance, let $X=\Bbb R$, and let $\tau$ be the usual topology on $\Bbb R$. Let
$$\mathscr{B}=\{(p,q):p,q\in\Bbb Q\text{ and }p<q\}\;,$$
the set of open intervals with rational endpoints; $\mathscr{B}$ is a base for $\tau$. Let
$$\mathscr{B}_0=\{(-x,x):0<x\in\Bbb R\setminus\Bbb Q\}\;,$$
the set of symmetric open intervals around $0$ with irrational endpoints. Clearly $\mathscr{B}_0\cap\mathscr{B}=\varnothing$, but $\mathscr{B}_0$ is nevertheless a nbhd base at $0$.
The best way to explain (3) is to give an actual example showing that it is possible to have a countable nbhd base at each point but no countable base for the whole topology. Probably the simplest example is to let $X$ be an uncountable set and $\tau=\wp(X)$ the discrete topology on $X$. For each $x\in X$ the collection $\big\{\{x\}\big\}$ is a nbhd base at $x$, and it's certainly countable! However, any base for $\tau$ must include every one of the singleton sets $\{x\}$ for $x\in X$ and must therefore be uncountable.
Comment: @J.C.VegaO: No: to show that the converse is not in general true, you must provide an actual counterexample to it.
Comment: @J.C.VegaO: Yes, that conclusion depends on the first assertion, not the second: you need the fact that every base for a topology contains a nbhd base at each point of the space.
Comment: @J.C.VegaO: The general argument is simply that if $X$ has a countable base $\mathscr{B}$, then for each $p\in X$ the family $\mathscr{B}_p=\{B\in\mathscr{B}:p\in B\}$ is a nbhd base at $p$, and since $\mathscr{B}_p$ is a subset of the countable set $\mathscr{B}$, it must also be countable. $X$ may also have uncountable bases, but we don't care about them for this result.
Comment: So, you are explaining (3) with a concrete example of what I said : that we could have an uncountable number of points in X, for which the union of countable neighbordhood bases might not be countable. If you were asking this on a exam, does it stand on its own without the example?
Comment: In (3), the initial part, which you didn't mention anything about  why countable basis implies countable neighborhood basis, is it correct to use (2) as they are doing?. Because I think it should be explained using (1) and not (2), what do you think? (I edited that part after you read it I presume)
Comment: Great, so you agree the subset-of-a-countable-set-is-countable argument correctly explains it? but  I have the feeling anyway that the corrected explanation relays on the fact of the nbhd basis being a subset of the basis  but you showed is not necessarily true.Then, something must be missing because this is an argument for a particular case, isn't it?. What would the general argument be?
Comment: ...at least somehow the general case could be reconduced to the particular one always


Join one table to another based on certain conditions met by rows in second table

Question: I have two tables. I want to create a SQL query such that for each row in Table 1, I want find the first two rows in Table 2 such ms.Table2 > ms.Table1. The resulting output will be Table 1 with six additional columns added showing ms, bid, ask from Table 2 corresponding to first two rows from Table 2 where ms is just greater than ms in Table 1.
Given the large number of rows, doing a cross-join and then filtering may be too much (since this has to be done many times over multiple data, not just once). Looking for an efficient solution. I am new to SQL so feel this is bit much for me, though I did try a cross-join. Please help!
Table 1 (2000+ rows)

ms
price
bid
ask

34200106
1.48
1.46
1.5

34200106
1.48
1.46
1.5

34202812
1.56
1.53
1.56

34202815
1.56
1.53
1.56

34202823
1.56
1.53
1.56

34204227
1.56
1.53
1.57

34207137
1.56
1.55
1.57

34208496
1.57
1.56
1.57

34208938
1.57
1.55
1.57

34208938
1.57
1.55
1.57

34208943
1.57
1.55
1.57

34208951
1.57
1.56
1.57

Table 2 (300000+ rows)

ms
bid
ask

34200009
1.46
1.51

34200011
1.46
1.5

34200106
1.46
1.5

34200171
1.46
1.5

34200195
1.46
1.5

34200195
1.46
1.51

34200201
1.46
1.52

34200202
1.46
1.52

34200276
1.46
1.52

34200296
1.46
1.52

34200305
1.46
1.52

34200306
1.46
1.52

34200308
1.46
1.53

34200326
1.46
1.53
Comment: There are duplicate rows in table 1. Should a row be returned for each duplicate or only for each unique row?
Answer: you could use a loop, but it will still be somewhat slow, but much faster than a cross join
CREATE TABLE table1
    ("ms" int, "price" int, "bid" int, "ask" int)
;

INSERT INTO table1
    ("ms", "price", "bid", "ask")
VALUES
    (34200106, 1.48, 1.46, 1.5),
    (34200106, 1.48, 1.46, 1.5),
    (34202812, 1.56, 1.53, 1.56),
    (34202815, 1.56, 1.53, 1.56),
    (34202823, 1.56, 1.53, 1.56),
    (34204227, 1.56, 1.53, 1.57),
    (34207137, 1.56, 1.55, 1.57),
    (34208496, 1.57, 1.56, 1.57),
    (34208938, 1.57, 1.55, 1.57),
    (34208938, 1.57, 1.55, 1.57),
    (34208943, 1.57, 1.55, 1.57),
    (34208951, 1.57, 1.56, 1.57)
;

CREATE TABLE

INSERT 0 12

CREATE TABLE table2
    ("ms" int, "bid" int, "ask" int)
;

INSERT INTO table2
    ("ms", "bid", "ask")
VALUES
    (34200009, 1.46, 1.51),
    (34200011, 1.46, 1.5),
    (34200106, 1.46, 1.5),
    (34200171, 1.46, 1.5),
    (34200195, 1.46, 1.5),
    (34200195, 1.46, 1.51),
    (34200201, 1.46, 1.52),
    (34200202, 1.46, 1.52),
    (34200276, 1.46, 1.52),
    (34200296, 1.46, 1.52),
    (34200305, 1.46, 1.52),
    (34200306, 1.46, 1.52),
    (34200308, 1.46, 1.53),
    (34200326, 1.46, 1.53)
;

CREATE TABLE

INSERT 0 14

CREATE OR REPLACE FUNCTION foo()
RETURNS TABLE ("ms" int, "price" int, "bid" int, "ask" int)
  language plpgsql as $$
DECLARE
  x int ;
  table1_cursor CURSOR FOR SELECT t1."ms" FROM table1 t1;
BEGIN
  CREATE TEMP TABLE IF NOT EXISTS temp_table AS
    SELECT *
    FROM table1;

  FOR x IN table1_cursor loop
    INSERT INTO temp_table ("ms", "price", "bid", "ask")
     SELECT t2."ms", NULL, t2."bid", t2."ask" FROM table2 t2
  WHERE t2."ms" > x."ms"
      ORDER BY t2."ms"
       LIMIT 2;
  END loop;

RETURN QUERY
SELECT t3."ms", t3."price", t3."bid", t3."ask"
from temp_table t3;
    DROP TABLE temp_table;

END $$;

SELECT * FROM foo();

CREATE FUNCTION

ms
price
bid
ask

34200106
1
1
2

34200106
1
1
2

34202812
2
2
2

34202815
2
2
2

34202823
2
2
2

34204227
2
2
2

34207137
2
2
2

34208496
2
2
2

34208938
2
2
2

34208938
2
2
2

34208943
2
2
2

34208951
2
2
2

34200171
null
1
2

34200195
null
1
2

34200171
null
1
2

34200195
null
1
2

SELECT 16

fiddle
Comment: Wow, thanks nbk, looks it might do the trick. I will need to study it carefully.
Comment: It goes over all rows of table1 and checks table2 for two rows that are bigger than ms , but you nee to go over all rows of table1 as long as you don"t have a limit to table1
Answer: The following query produces the described results without doing a full cross join between table1 and table2:
SELECT table1.ms,
       table1.bid,
       table1.ask,
       t2.ms[1] AS ms1,
       t2.bids[1] AS bid1,
       t2.asks[1] AS ask1,
       t2.ms[2] AS ms2,
       t2.bids[2] AS bid2,
       t2.asks[2] AS ask2
  FROM table1
  CROSS JOIN LATERAL (SELECT ARRAY_AGG(t.ms) AS ms,
                             ARRAY_AGG(t.bid) AS bids,
                             ARRAY_AGG(t.ask) AS asks
                        FROM (SELECT table2.*
                                FROM table2
                               WHERE table2.ms > table1.ms
                               ORDER BY table2.ms
                               LIMIT 2
                             ) t
                     ) AS t2;

Comment: Great solution. Quite fast relative to a full cross-join. Thanks


Merge two rows in columns based on condition

Question: I am new here, as well as to R, and I couldn't find any past queries that answered my following question, so I apologise if this has already been brought up before.
I am trying to merge the ID columns from two different datasets into one, but some of the IDs in the rows have been coded differently. I need to replace all the "LNZ_" IDs with the "LNZ.", however, I cannot figure out how I would go about doing this.
df_1 <- data.frame(ID_1 = c("LNZ_00001", "LNZ_00002", "LNZ_00003", "DFG00001", "CWD00001"),
                   Sex=c("M","F","F","M","F"))

df_2 <- data.frame(ID_2 = c("LNZ.00001", "LNZ.00002", "LNZ_00003", "DFG00001", "CWD00001"),
                   Type=c("S","S","B","B","B"),
                   AGE=c(56,75,66,64,64))

The above is similar to the datasets that I have, only more scaled down. I hope this is somewhat clear, and any help would be appreciated.
Thanks!
Comment: `df_1$ID_1 <- sub("LNZ_", "LNZ.", df_1$ID_1)`, which has nothing to do with `merge`, but after that then merge(df_1, df_2, by.x="ID_1", by.y="ID_2", all=TRUE)` works without issue.
Answer: The issue with merging is that your ID columns have different formatting for some of the entries which are supposed to be matched. Therefore you need to modify those values to match before performing the merge. In the examples you gave, the difference is between a period separator (.) and an underscore (_). If your real data has more complex issues, you may need to use different code to clean up those values.
However, once that is resolved, you can perform your merge easily. Here I've used the {tidyverse} packages to accomplish both steps in one pipe chain.
library(tidyverse)

df_1 <- data.frame(ID_1 = c("LNZ_00001", "LNZ_00002", "LNZ_00003", "DFG00001", "CWD00001"), Sex=c("M","F","F","M","F"))

df_2 <- data.frame(ID_2 = c("LNZ.00001", "LNZ.00002", "LNZ_00003", "DFG00001", "CWD00001"), Type=c("S","S","B","B","B"), AGE=c(56,75,66,64,64))

df_2 %>%
  mutate(ID_2 = str_replace(ID_2, "\\.", "\\_")) %>%
  left_join(df_1, by = c("ID_2" = "ID_1"))
#>        ID_2 Type AGE Sex
#> 1 LNZ_00001    S  56   M
#> 2 LNZ_00002    S  75   F
#> 3 LNZ_00003    B  66   F
#> 4  DFG00001    B  64   M
#> 5  CWD00001    B  64   F

Created on 2022-07-17 by the reprex package (v2.0.1)
Comment: FYI @DanAdams, the topic of politely reminding new users to accept questions has been discussed many times on meta, and over the last year or so the mindset has changed a little. The [current stance](https://meta.stackoverflow.com/a/417578/584676), and while I refute the effectiveness of their "improved new features", it is likely that your comment here may be removed (flagged as not relevant). I caution against doing this repeatedly, as it might lead towards a nasty-gram email asking you to desist. Just a friendly note.
Comment: @r2evans - thanks for that note. It always feels like an uncomfortable blend of self-serving community maintenance. Will avoid in future.
Comment: If this solved your issue, please accept the answer so the community will know it's closed and others can benefit from it.
Comment: @DanAdams, Thanks for the reminder, I was trying to figure out how to do that, but I got there in the end :)
Comment: I often lean more towards community maintenance, but the moderators either disagree or have their hands tied. Either way, I feel their assumptions are incorrect yet maintenance and polite reminders are suppressed. Having said that, many users _clearly_ are fishing for having their answer accepted, so I also believe wholeheartedly that requesting "accept" needed to be done with care. Either way, they haven't asked my opinion :-)


Once SPF is correctly setup is it normal for spoofed email to go to spam folder or should Gmail deleted them?

Question: In the spam folder of Gmail can be found illegitimate spam emails apparently sent from my own domain email address but from an origin not allowed by SPF. The domain is on G Suite emails. SPF is allegedly correctly setup as "v=spf1 include:_spf.google.com ~all". What is the expected behavior of Gmail in that case. Should it delete those emails so they do not even reach the spam folder or should they appear in spam.
Comment: Side note: with `~all`, you're telling Gmail "I'm not sure about my SPF record, let everything through". A pass may *reduce* the spam score, but a fail will not increase it. You want `-all` for it to penalize failing emails.
Answer: Given the ease of messing up an SPF record, Gmail appears to consider a failing SPF check as a major factor for the spam score of an email, but it's possible for it to still wind up in the inbox.
To my knowledge, Gmail will not just silently delete an email by default.
If you're in a Google Apps install, there may be rules the administrator put in place. This doc indicates a DMARC record may be used to have SPF-fail email deleted.
Comment: Thanks for taking time on a Sunday to help a stranger! The info is exactly what I needed. Yes the domain is on G Suite emails by Google.
Comment: I'm pretty sure Gmail does not silently delete emails. However in some cases Gmail will refuse to receive an email in the first place. To the receiver that will look the same as if it was deleted. The sender will eventually receive an error message about failed delivery.


Placing rotated label on Form

Question: I am having problems correctly placing my object, i want to achieve corner ribbon, something like this:

Because my ribbon should be 1/5 of a whole form i devided form into 5 columns:
<code><Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid Grid.ColumnSpan="5">
    </Grid>
    <Border Margin="0" BorderThickness="2" Height="auto" BorderBrush="White" Background="#FFE12727" HorizontalAlignment="Stretch" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto" Padding="0,0,6,0">
        <Border.RenderTransform>
            <CompositeTransform Rotation="-45"/>
        </Border.RenderTransform>
        <Viewbox VerticalAlignment="Center" Margin="50,15,50,10">
            <TextBlock VerticalAlignment="Center" Text="NEW HERE?"/>
        </Viewbox>
    </Border>
</Grid>
</code>
It looks like this:

How should i set the margins, padding etc, so that ribbon on all resolutions remained like on 1 picture?
Or any different way to achieve this?
Comment: You could also put an image (png with transparent area) on top of your grid. This is of course only practical if the label text is static.
Answer: You can use the CenterX and CenterY properties to set the center of rotation for the object. In your case the rotation center is in the middle of the border - playing a bit with these properties and it should work
For example, if you wanted a 100 by 100 square to pivot around its center, you would set the CenterX and CenterY properties to 50.
by http://msdn.microsoft.com/en-us/library/system.windows.media.compositetransform.rotation(v=vs.95).aspx

Placing rotated label on Form


Notice: Object of class PDOStatement could not be converted to int in

Question: Eu não entendo muito da parte de PHP e estou com um probleminha que não estou conseguindo resolver.
ERRO no site
Notice: Object of class PDOStatement could not be converted to int in orcamento.php on line 63

Codigo referente a linha que esta dando erro
Deixei marcado onde esta localizada a linha 63
    <?php
                    $sql_sb = "SELECT * FROM tbl_album WHERE ALBUM_STATUS = 1 ORDER BY ALBUM_NOME ASC";
                    $b = $con->query($sql_sb);
                    $nRows = $con->query($sql_sb);
                    $i = 3;
                ?>

                <?php
                **linha 63 aqui**   if($nRows > 0){
                        while($busca = $b->fetch(PDO::FETCH_ASSOC)){
                            if($i % 3 == 0){
                                echo '
';
                            }
                                echo '';
                                echo '
<input type="checkbox" value="'.$busca['ALBUM_NOME'].'">'.$busca['ALBUM_NOME'].'
';
                                echo '	';
                            //echo '
';
                            $i++;
                        }
                    }else{
                        echo "Não há produtos cadastrados";
                    }
                ?>

Obrigado
Answer: Olá. Faça a seguinte alteração:
$nRows = $con->query($sql_sb);

Para
$nRows = $b->rowCount();

Se você perceber, $nRows tem o mesmo valor de $b, então ao invés de você recuperar o número de colunas, está re-executando a mesma query.
Comment: Nossa, que vacilo o meu, realmente não reparei nessa parte.
Comment: Muito obrigado, ajudou muito !!!
Comment: Bastante normal isso, ainda mais no começo. É assim que se aprende. Boa sorte nessa caminhada.


Fazendo JOIN no Pandas com intervalos de tempo

Question: Tenho os seguintes dataframes:
df_equip

Equipamento
Data
Operador

X
2022-01-03 12:09:00
NaN

X
2022-01-03 12:19:00
NaN

X
2022-01-03 12:29:00
NaN

X
2022-01-03 13:19:00
NaN

Y
2022-01-03 12:13:00
NaN

Y
2022-01-03 12:34:00
NaN

X
2022-01-03 13:49:00
NaN

X
2022-01-03 14:49:00
NaN

X
2022-01-03 15:29:00
NaN

X
2022-01-03 18:49:00
NaN

df_login

Operador
Status
Data
Equipamento

A01
Login
2022-01-03 12:10:00
X

A02
Login
2022-01-03 12:11:00
Y

A02
Logout
2022-01-03 12:14:32
Y

A01
Logout
2022-01-03 13:15:00
X

A03
Login
2022-01-03 14:10:00
X

A03
Logout
2022-01-03 16:10:00
X

Eu preciso inserir na df_equip['Operador'] o código relacionado ao operador contido em df_login['Operador'] levando em consideração o tempo em que ele está com o login ativo. Sendo que a coluna DATA dos dois dataframes contém valores distintos.
Gostaria de saber se há algum jeito de usar esse intervalo de tempo entre login e logout para fazer o JOIN entre as duas tabelas e inserir o valor que eu preciso.
PS: O equipamento pode estar ligado sem nenhum operador logado
Comment: GusThePraga, conseguiu verificar se minha resposta atendeu a sua pergunta? Se sim, considere [aceita-la](https://pt.meta.stackoverflow.com/a/1079/30452) :)
Answer: Infelizmente ainda não existe uma forma nativa no pandas para unir 2 DataFrames por um intervalo de datas.
Para o seu problema eu resolveria isso com os seguintes passos:

Transformar as colunas Data para o tipo datetime com to_datetime
Remover a coluna Operador do df_equip com o drop
Transformar o Status em colunas com a fução pivot
Unir os DFs com .merge pela coluna Equipamento
Criar coluna dummy para saber quem estava loggado no momento com .between
Mudar para NaN o Operador quando ele não estava logado com mask
E remover os itens duplicados com .drop_duplicates, .sort_values e .drop

Segue o código:
df_equip['Data'] = pd.to_datetime(df_equip['Data'])
df_login['Data'] = pd.to_datetime(df_login['Data'])

df_equip.drop(columns=['Operador'], inplace=True)

df_login = df_login.pivot(index=['Operador', 'Equipamento'], columns='Status', values='Data').reset_index()

new_df = df_equip.merge(df_login, on = 'Equipamento', how = 'left')

new_df['is_logged'] = new_df['Data'].between(new_df['Login'], new_df['Logout'])

new_df = (
     new_df.sort_values('Operador')
    .drop_duplicates(['Equipamento', 'Data'])
    .drop(columns=['Login', 'Logout', 'is_logged'])
    .sort_values('Data')
)

new_df.head(10)
#saida:
    Equipamento Data                Operador
0   X           2022-01-03 12:09:00 NaN
8   Y           2022-01-03 12:13:00 A02
2   X           2022-01-03 12:19:00 A01
4   X           2022-01-03 12:29:00 A01
9   Y           2022-01-03 12:34:00 NaN
6   X           2022-01-03 13:19:00 NaN
10  X           2022-01-03 13:49:00 NaN
13  X           2022-01-03 14:49:00 A03
15  X           2022-01-03 15:29:00 A03
16  X           2022-01-03 18:49:00 NaN


Configure FastCGI for PHP on Low MEM VM

Question: I have a VM with only 256MB of RAM that is running nginx and PHP 5.3. Since I am resource constrained (cannot change the amount of RAM in the VM) I am looking into the various ways of setting up FastCGI for PHP. Perhaps my Google-fu is weak, but I cannot seem to find much about my options when setting up FastCGI or any best practices advice.
I am interested in being direct to information on:

What FastCGI servers options are there?
Are any of these clearly better or worse than the others?
Considering my setup (256MB RAM, nginx) are there any clear resources or technical blogs that I should be following for advice?
Answer: I recommend using php-fpm to manage the fastcgi processes.
There are a lot of options to control the number of childs etc.
http://dotdeb.org has nice packages for Ubuntu/Debian.
For the basic setup see http://interfacelab.com/nginx-php-fpm-apc-awesome/

Configure FastCGI for PHP on Low MEM VM


Load a text file paragraph into a string without libraries

Question: sorry if this question may look a bit dumb for some of you but i'm totally a beginner at programming in Python so i'm quite bad and got a still got a lot to learn.
So basically I have this long text file separated by paragraphs, sometimes the newline can be double or triple to make the task more hard for us so i added a little check and looks like it's working fine so i have a variable called "paragraph" that tells me in which paragraph i am currently.
Now basically i need to scan this text file and search for some sequences of words in it but the newline character is the worst enemy here, for example if i have the string = "dummy text" and i'm looking into this:
"random questions about files with a dummy
 text and strings

 hey look a new paragraph here"

As you can see there is a newline between dummy and text so reading the file line by line doesn't work. So i was wondering to load directly the entire paragraph to a string so this way i can even remove punctuation and stuff more easly and check directly if those sequences of words are contained in it.
All this must be done without libraries.
However my piece of code of paragraph counter works while the file is being read, so if uploading a whole paragraph in a string is possible i should basically use something like "".join until the paragraph increases by 1 because we're on the next paragraph? Any idea?
Comment: What is your variable `paragraph`? Is your `"random ... here" is a paragraph?
Comment: @GyuHyeonChoi I've not posted the code here but the variable paragraph is just a integer that increases each time finds more words after 1 or more newspaces. For example in this case from "random" to "strings" is paragraph 1, then from "hey" to "here" is paragraph 2
Comment: More than 1 newlines separate paragraphs? And what you want to do is find sequences of words inside a paragraph?
Comment: @GyuHyeonChoi Sometimes can happen that the newline can be more than 2 in order to make this whole thing more complex (it's a university project). Basically yes, i need to find if exactly those sequences of words are in that paragraph (in sequence obviously, if "dummy" and "text" weren't next to each other the output should be false)
Comment: I think you can just `join` when you encounter an empty line?
Comment: @GyuHyeonChoi Damn didn't think about that, gonna try a piece of code and see if works
Comment: Please check my answer as well...!
Answer: This should do the trick. It is very short and elegant:
with open('dummy text.txt') as file:
    data = file.read().replace('\n', '')
print(data)#prints out the file

The output is:
"random questions about files with a dummy text and strings hey look a new paragraph here"

Comment: This replaces \n and the issue of \n during the check is solved but i still need to keep track of the paragraph counter, it's part of the return value, any idea?
Comment: I did not get how your code works. I f possible could you share the code you wrote to output the para. Maybe then I can find a way to connect them. Thanks,
Answer: You can strip the newline character. Here is an example from a different problem.
data = open('resources.txt', 'r')
book_list = []
for line in data:
    new_line = line.rstrip('\n')
    book_list.append(new_line)

Comment: Yes the .rstrip was something i was definitively going to do i just need to figure out how to merge together all the lines of 1 paragraph to a string and after i've done all the operations that needed to be done load another paragraph to the string and so on until the file is finished
Comment: @Krist Let's say you have a string called my_string which may or may not be empty.  You just add my_string += new_line inside the for loop and it will build your string.
Answer: I think you do not need to think it in a difficult way. Here is a very commonly used pattern for this kind of problems.
paragraphs = []
lines = []
for line in open('text.txt'):
    if not line.strip():  # empty line
        if lines:
            paragraphs.append("".join(lines))
            lines = []
    else:
        lines.append(line)
if lines:
    paragraphs.append("".join(lines))

If a stripped line is empty, you encounter the second \n and it means that you have to join previous lines to a paragraph.
If you encounter the 3rd \n, you must not join again so remove your previous lines (lines = []). In this way, you will not join the same paragraph again.
To check the last line, try this pattern.
f = open('text.txt')
line0 = f.readline()
while True:
    # do what you have to do with the previous line, `line0`
    line = f.readline()
    if not line:    # `line0` was the last line
        # do what you have to do with the last line
        break
    line0 = line

Comment: With a few changes but keeping this concept I integrated this into my program but there's only 1 issue: when i reach the end of the file (the last paragraph) since there's no newline it just ignores the .join at that point, is there any way to understand if i'm at the last row of a file? I've opened it like this:
    for index, line in enumerate(file.readlines()):
Comment: In my code snippet. The last `if` outside the `for` loop is to `join` the last paragraph. The last element of the list returned by `readlines()` is the last line. After the last line `readline()` returns `None`.
Comment: Yes however i had to do some changes to integrate this in my program and couldn't take that last part, just did another piece of code before this to read the file and save the last line of it and added an "if" during the paragraph checks that in case line == last_line just do the .join, after i'll make this whole thing work i'll start the optimization process
Comment: @Krist I added another pattern for your requirement. Please check it and accept it if it works! :)
Comment: Worked perfectly! Still stuck because of the complexity of this exercise but i hope to find a way through it, I underestimated the "words in sequence" part and words that simply are in the file singularly are in my output... maybe tomorrow will ask for more help because this is driving me crazy, it's been more than 1 week already
Comment: @Krist Any more questions?
Comment: I got a question about the list.pop(), I'm trying to figure out how to clean my list from stuff that doesn't exist at all in a single paragraph so i was thinking about using a common cycle `for i in range(len(list))` and in case the element is not found i just use list.pop(i). But i was wondering how can i make sure to make the "for" cycle from the same index? Let's say i removed list[0] because wasn't in the string, now list[1] has moved to list[0] but i need to check that again
Comment: @Krist Very simple and important solution: loop **backward**.
Comment: so basically i just do i -= 1?
Comment: @Krist `for i in range(len(list) - 1, -1, -1):`, haha. If `list[i]` is empty, pop it out and go to `list[i - 1]`
Comment: Regarding the code for the last line that you gave me yesterday, i just noticed through debug that for some reason it just passes from first line of the txt to second without going trough the conditions, is there any way to take the first line into conditions aswell? This is a piece of code maybe you can understand better, is a bit written in a quite confused way: https://pastebin.com/RrDDbDu9
Comment: @Krist You do not need both of `while` and `for`.
Comment: Hey again me, sorry if i'm bothering you but this stuff is driving me crazy i need desperate help. I'm writing the piece of code that is supposed to check if all words in my list of sentences are contained in my string and i'm at this point: https://pastebin.com/bu7A7ZKx
Basically it keeps giving me an error `list index out of range at gr_clean_word = words[c].strip(".,:;!?")` but why?
The other thing i'm worried about is that i can't make the continue statement pass into next word if it find any translation and instead if any other translation happen to be wrong will delete the word anyway
Comment: @Krist Why do you do `c -= 1`. `c` is decrementing from `len(words) -1` to `-1`. If you additionally subtract 1 from `c`, `c` will be out of `range`. Please accept my answer if helpful!
Comment: removed them both, now it says words.pop(c) index out of range
Comment: @Krist Hope you to consider accept the answer or reject it. Anyway, if you use another `for` loop in sides the main loop, you could `pop` more than you need.
Comment: I accepted it but i'm below 15 of reputation so that's recorded but not shown until i reach 15 of reputation i think :\
Comment: @Krist Thanks, buddy! Solved your issue?
Comment: With debug I tried to understand what's happening and basically the 2nd problem that i explained above is the cause of all of this: the cycle should just work fine (since is backwards) but it basically deletes a word each time translation in dict has not been found (pastebin from line 24) but what i need is to check if any translation can be found in dict items, if yes he just should do nothing and only if none of items can be a translation delete the word and go back and cycle next word (line 13 of the pastebin)
Comment: @Krist In such a pattern ("there exists" or "at least one" condition), how about to `pop` then `break`?
Comment: the break worked perfectly, thanks for the tip. The "exists" is something included in .os library and we're not allowed to use it so i need to figure out another way to make the word stay in the list if at least 1 item in dictionary corresponds
Comment: @Krist I meant `∃`. For "there exists at least one which satisfies the condition" problem, you can break right after the condition is met. It is also good for performance.
Comment: Played a bit with "break" and "continue" and look like it's working just fine now!
https://pastebin.com/jsEGMXQE


Gson HTTP response object

Question: I am using Gson library for first time. I am making an HTTP request and pulling response (JSON response) and need to pull a specific result.
StringBuilder response;
try (BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream()))) {
    String line;
    response = new StringBuilder();
    while((line = in.readLine()) != null) {
        response.append(line);
    }
}

Gson gson = new GsonBuilder()
                .setPrettyPrinting()
                .create();
System.out.println(gson.toJson(response));

The response looks like this below, and I need to pull only cardBackId:
[{\"cardBackId\":\"0\",\"name\":\"Classic\",\"description\":\"The only card back you\u0027ll ever need.\",\"source\":\"startup\",\"sourceDescription\":\"Default\",\"enabled\":true,\"img\":\"http://wow.zamimg.com/images/hearthstone/backs/original/Card_Back_Default.png\",\"imgAnimated\":\"http://wow.zamimg.com/images/hearthstone/backs/animated/Card_Back_Default.gif\",\"sortCategory\":\"1\",\"sortOrder\":\"1\",\"locale\":\"enUS\"

Comment: Are you trying to convert a JSON string into an object?
Comment: gson.toJson(response) is pulling the full response. I only want to pull the "cardBackId".
Comment: StringBuilder resp;

            try (BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()))) {
                String line;
                resp = new StringBuilder();

                while((line = in.readLine()) != null) {
                    resp.append(line);
                }
            }
Comment: `toJson()` converts an object to a JSON string. `fromJson()` converts a JSON string to an object.
Answer: You could use JSONPath (which is a Java library for selecting parts of a JSON object) to extract just the part you need from the string.
Alternatively, you could write a class that only contains the field you want:
public class CardBackIdResponse {
    public int cardBackId;
}

And then use Gson to unmarshall the JSON into your object:
CardBackIdResponse[] cardBackIdResponses = gson.fromJson(response.toString(), CardBackIdResponse[].class);
System.out.println("cardBackId = " + cardBackIdResponses[0].cardBackId);

When unmarshalling an object from JSON, if Gson cannot find a field in the object to populate with a value from the JSON, it will just discard the value. That's the principle we could use here.
Edit: Altered answer above to handle JSON array as per this SO question.
Comment: More docs on how to use Gson: https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Comment: I receive error below when using your suggested solution.

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
Comment: Edited answer to handle that `response` is actually a JSON array.
Comment: I was able to resolve.

            CardResponse[] list = gson.fromJson(response.toString(), CardResponse[].class);
            System.out.println(gson.toJson(list[1]));

Gson HTTP response object