text file to bitmap in allegro

Question: hello what I'm currently dealing with is the ability to get input from a text file and then convert it into a bitmap and save it to a file.
the input looks like this:
########
#      #
########

and I want to draw it using allegro and instead of # there would be pixels of specified size. Each # should represent a tile (10x10 pixel). So the final result would look like this
link to an image
I've actually drawn it using this code:
 for (int i = 0; i < 80; i++){
        for (int j = 0; j < 10; j++){
            al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
        }
    }
    for (int i = 0; i < 10; i++){
        for (int j = 10; j < 20; j++){
            al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
        }
    }
    for (int i = 70; i < 80; i++){
        for (int j = 10; j < 20; j++){
            al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
        }
    }
    for (int i = 0; i < 80; i++){
        for (int j = 20; j < 30; j++){
            al_draw_pixel(i, j, al_map_rgb(0, 0, 0));
        }
    }

yeah that's pretty bad, so how do I achieve something like that but with a common procedure which would be independent on the text file? thanks for any advice.
note: the only allowed headers are allegro5/allegro.h and allegro5/allegro_image.h
Answer: To draw to an image with Allegro 5, you need to do something like:
ALLEGRO_BITMAP *bmp = al_create_bitmap(640, 480);
al_set_target_bitmap(bmp);

Now all of your drawing operations will happen on the image. To later save it:
al_save_bitmap("somefile.bmp", bmp);

You can also use png and jpg as extensions if your image library has support for it enabled.
Use these functions to read the text file:

al_fopen
al_fgetc
al_feof
al_fclose

Set int x and y to zero. You'll be looping over until the end of the file. On every iteration increment x by one. If you reach a new line character (\n) increment y by one and set x to zero. (You should ignore \r characters.)
Now, depending on the character read, draw a tile:
ALLEGRO_BITAMP *tile_to_draw = NULL;
if (c == '#')
   tile_to_draw = bmp1;
else if (c == ' ')
   tile_to_draw = bmp2;

if (tile_to_draw)
   al_draw_bitmap(tile_to_draw, x * 10, y * 10, 0);

Of course there's better ways to map characters to tiles than a series of ifs, but the above works and should be enough to help you finish your homework.
Comment: where do I store those values? or I don't have to store them at all? like int c; or an array of char to store all those characters? also what are those bmp1 and bmp2?
Comment: also there is an error with the x counter.. since the al_feof return EOF so x takes EOF and has value of 2. How to fix that?
Comment: `c` represents the value of `al_fgetc` for the current iteration. You really should be able to get a working solution based on my code. If not, you probably need more help than this site is really designed to provide, and you should consider getting help at some forums, such as http://www.allegro.cc/forums.

text file to bitmap in allegro


SQUID Proxy "The request or reply is too large"

Question: When I am using squid proxy in the server, I get an error message, "The request or reply is too large" from any node in the LAN.
How can I solve this?
Answer: This error message appears when you try downloading large file using GET or uploading it using POST/PUT. There are several parameters to look for:
request_body_max_size

reply_body_max_size

These two are set to 0 by default, which means no limits at all. They should not be limited unless you really know how that affects your squid behavior. Or at all in standard proxy.
request_header_max_size

reply_header_max_size

These two default to 64kB starting from Squid-3.1. Earlier versions of Squid had defaults as low as 2 kB. In some rather rare circumstances even 64kB is too low, so you can increase this value.
from wiki sqid faq


What are some good examples of announcing new features within a mobile app?

Question: I'm designing a new feature for my company's mobile app, and I want to make a splash with a brief announcement explaining what the feature is and how to use it. This announcement would appear in the app after the user downloads the update.
Are there relevant apps out that I could look to for inspiration?
Comment: will you have a "what's new" or similar message on google play or appstore?
Answer: Check this out: http://inspired-ui.com/tagged/walkthroughs
These are mostly showing first-time-use scenarios, but the same idea can be extended for new features.
Answer: A good way to introduce new features to users in mobile app is to use instructional overlays or coach marks (a transparent overlay of UI hints).

Take a look at this article to see some helpful guidelines on how to implement these UI hints:
http://www.nngroup.com/articles/mobile-instructional-overlay/
Comment: This isn't really ideal because users won't be able to remember all of those tips since it was all introduced to them all at once.
Answer: I really like the approach Slack has taken with "hot spots" showing up as you start using the app. While it's annoying after you've been through it a couple of times (joining a new team site triggers the walkthrough for each specific team), it's very effective with how it introduces the functionality.


How to switch workspace with mouse wheel in GNOME Shell

Question: A system like the workspaces in Unity7 had an icon on the dock or something similar that give you the possibility of cycle through virtual desktops with mouse buttons or wheel.

For who is not familiar with Unity7 workspaces and virtual desktops, this is a useful video to understand.
How can I do the same on GNOME Shell (17.10+)?
Comment: You should make your question more clear and easier to understand, see as well [how to ask a good question](//askubuntu.com/help/how-to-ask). Thank you.
Comment: Are you asking about how to switch between different workspaces while remaining in the same desktop environment?
Comment: I edited the question, hope it's more clear now.
Answer: Place the mouse pointer on Show Applications icon and scroll with the wheel:

On keyboard: Ctrl + Alt + Arrow Keys (unless you have Horizontal workspaces)

2022-11: It seems this isn't working anymore (not sure when). This should always work.
Comment: I've done this accidentally so many times and could never figure out how I did it. Thanks!
Comment: I came here becuse, like @jac said, I did it by accident. It's definitely working on Ubuntu 22.04
Answer: Click on "Activities". In the Activities overview use your mouse wheel or touchpad scroll switch between workspaces.
Comment: My fault, it's the classic GNOME behavior!
Answer: You can also use the Desktop Scroller GNOME Shell Extension which will let you "Switch between workspaces by scrolling at the edges of the screen or over the desktop background."
Answer: At least as of Ubuntu 22.04, you can hold down the Super/Windows key and use the mouse scroll wheel to switch between workspaces.
Answer: You can use my repository to navigate using Alt+ scroll between workspaces:
https://github.com/bit-willi/gnome-navigation-between-workspaces-using-mouse-shortcut
Answer: Scrolling the mouse wheel while the mouse pointer is over the Show Applications button will change the workspace
ubuntu 20.04 screen shot


How to display relevant record first using FULLTEXT in SQL?

Question: I am using the FULLTEXT in my sql query to search the records.
        SELECT * FROM (SELECT d.DataId, (SELECT [Name] FROM Category WHERE CategoryId = d.CategoryId) AS 'Category', d.Description, d.CompanyName, d.City, d.CategoryId,
                d.CreatedOn, d.Rank, d.voteCount, d.commentCount, d.viewCount
        FROM Data d
                INNER JOIN Keyword k
                    ON d.DataId = k.DataId
        WHERE FREETEXT(k.Keyword, @SearchQ) AND d.CategoryId=@CategoryId AND d.IsSearch=1 AND d.IsApproved=1

When I am searching for rockstone hotel, I have a record with the same name in my table but it displays some other record first and then displays "rockstone hotel's" record at 3 position. Why its happening and what are the way's to improve this ???
I'm using SQL Server 2005
Thanks
Answer: You have no Order By clause, which frees the database to return records in any order. Usually it is the order in which it encounters them as it processes the where clause, so it is easy to believe there is a "natural" order that the database uses. If you want a specific order you either need to add an Order By clause that will use some scoring method you create or you must order them in the program that receives the records.
(I would have to guess that the other records you are pulling also contain the search criteria, just farther into the text field.)
See here for an example of using Rank to order your records: SQL Server Freetext match - how do I sort by relevance  The Rank column is generated by the text matching call.
Comment: Can't we sort the records on the basis of relevancy with searched-keyword ??
Answer: Quoted from MSDN (SQL Server Developer Center):

Unlike in the CONTAINS search
  condition where AND is a keyword, when
  used in freetext_string the word 'and'
  is considered a noise word and will be
  discarded.

http://msdn.microsoft.com/en-us/library/ms176078(SQL.90).aspx#


firefox-locale-en

Question: There is a package called firefox-locale-en on my system (Ubuntu 20.04 lts), even though I have uninstalled Firefox on the very first day. Is it safe to purge it with sudo apt purge firefox-locale-en?
EDIT 1
If yes, what does it do?
EDIT 2
Even though it might sound like a silly doubt, but I would always like to have a knowledgeable person's opinion, because I remember last time python3.6 was not used by me specifically and I decided to purge it. Only to find out my PC doesn't work at all any more. And Hence I got to 20.04 from 18.04. Same way I thought firefox might be associated with some internet related service on Ubuntu.
Comment: It doesn't do anything. It is English localization for FF.
Comment: You can remove it.
Comment: what does it do anyway?
Answer: A locale is a "set of parameters that defines the user's language, region and any special variant preferences that the user wants to see in their user interface -- usually a locale identifier consists of at least a language identifier and a region identifier." (Source Wikipedia)
It is safe to remove the firefox-locale-en package because since you aren't using Firefox you have no use for firefox's locale.

firefox-locale-en


How can I set up Unity 3 to work in a console application?

Question: I would like to get some experience using Unity 3 so I set up a console application and downloaded Unity from nuGet.
However I didn't find any examples on how I can start Unity? Do I need to add some code to my main method to make a Unity container or is it normal to put that code into another file and call that?
Note that I did find the "Unity hands-on labs" but these look far too complicated for a starter tutorial. I would really like to find some starter example coded in C# if anyone has any links bookmared that would be a great help.
Answer: http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx
Basically, yes, you have to initialize Unity first. Just put this in the beginning of your Main method:
var container = new UnityContainer();
container.RegisterType<IYourType, YourType>();


Android: Change to unsupported locale

Question: I been trying to change locale using the next code:
private void changeLocal(Locale locale){
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
          getBaseContext().getResources().getDisplayMetrics());
    refresh();
}

private void refresh() {
    finish();
    Intent myIntent = new Intent(this, getClass());
    startActivity(myIntent);
}

It works for French but not for Hebrew. So I looked in to device supported locales by calling Locale.getAvailableLocales() and I found out that French is there but Hebrew not.
If I put hardcoded Hebrew text I can see it, so it is installed, but I do not know how to force the device to use it.
Please help me force the device using Hebrew.
P.S
Wasn't been able to make this work without the refresh even when adding
android:configChanges="locale|layoutDirection"

to my Manifest it does not call the onConfigurationChanged of my Activity
Comment: Hebrew's locale is `iw`, not `he`
Answer: Since there are 2 distinct standards, you have to double your values folder.
Add your Hebrew strings to these folders:
values-he

and
values-iw

Some devices will use one folder. Other devices will use the other one.
Note that he is actually deprecated. So, most devices will use iw

Android: Change to unsupported locale


Arrangement of points in a circle

Question: From the 2015 Moscow Mathematical Olympiad:

The numbers $1$ to $1000$ are arranged on a circle such that each number divides the sum of its two neighbors. Suppose that the number $k$ has two odd neighbors. What's the parity of $k$ ?

I haven't been able to answer the question just yet.
Here are a few things I noticed. The constraint "$x$ divides its two neighbors $a$ and $b$" gets tighter when $x$ grows. For example, if $x=1000$, we must have $1000=a+b$. If $x=999$, we have either $999=a+b$ or $1998=a+b$ (in that case $(a,b)\in \{(1000,998),(998,1000)\}$).
Nevertheless, I can't derive anything useful from $k| a+b$ with $a,b$ being odd numbers.
Answer: diagram
the numbers from 1 to 1000 have equal number of even(E) and odd(O) numbers.
Let us assume an arrangement where there is at least one group of an odd number with two odd neighbors.This is arrangement A.
So we have 3 odds one after another. Let all evens in A have two odd neighbors. But then if we group two numbers in the anticlockwise direction we end up with an odd number for every even number, along with some odds extra.Thus it is not possible.
Again let in A, we have some evens with one odd & one even neighbor instead of all evens with odd neighbors. But since even doesn't divide even+odd, so this arrangement too is futile.
Thus, for our given arrangement A, we should have an even number with 2 even neighbors, like: ......,E1,E2,E3,..... In such case,E3 must be followed by E4,
for otherwise we have ...,E1,E2,E3,O,.... but E3 can't divide E2+O. Again, for E4 we must have a consequent E5, and for E5 we must have E6 ;thus this makes all numbers on the circle even,which is impossible.
*Note that we have exhausted all variations of arrangement for A, each time getting a contradiction. Thus arrangement A is impossible.
Therefore, a number with two odd neighbors must be even.*
Answer: Just write out the numbers is their order from $1$ to $1000$ on the circle and the condition is satisfied. The answer to the question is now even. You will have $1000$ between $1$ and $999$ and every other number $n$ will have $n+1$ and $n-1$ whose sum is $2n$.
Comment: You're assuming (without a proof and just because the question ("what's the parity of $k$?") seems to tell you so) that $k$ must always have the same parity under all arrangements. Now you're looking at a special case (when $1,2,3,\ldots,1000$ are in this order) to find the parity. Just because $k$ is even under this arrangement of $1,2,3,\ldots,1000$ doesn't mean $k$ will always be even under other arrangements.
Comment: @LeGrandDODOM Come up with an arrangement that exists other than the one mentioned.
Comment: Without a proof that $k$ is even when no assumption is made on the arrangement, your "answer" should be a comment, at best. And thanks for the downvote.
Comment: @user236182 Well, an olympiad question cannot have two answers. That's for sure. When you are asked 'what is the parity of k?' You can't reply 'Sometimes even, sometimes odd..' can you?

Arrangement of points in a circle


Bootloader install failed - RAID

Question: I am trying to install Ubuntu 13.04 on a desktop with hardware raid (3 disks in raid 5). I believe I have an ICH8 chipset, if that is relevant.
So, I created an install USB stick with Startup Disk Creator with the amd64 Ubuntu Desktop image. I proceed through the install, select to erase everything and install Ubuntu (no lvm, no encryption, or anything like that). The install seems to go fine, until the very end, I get an Error that says "Failed to install GRUB"
Unable to install GRUB in

I think it initially tried /dev/sda, but then I got a menu where I could select a net partition. I tried other selections, but nothing seemed to work. Do I need to unmount something to install GRUB?
Screenshot with other install path candidates:
Bootloader install failed

Here is the output of ls -l /dev/mapper
ubuntu@ubuntu:~$ ls -l /dev/mapper
total 0
crw------- 1 root root  10, 236 Aug 25 06:48 control
brw-rw---- 1 root disk 252,   0 Aug 25 14:17 isw_bifaeaafcc_Volume0
brw-rw---- 1 root disk 252,   1 Aug 25 14:17 isw_bifaeaafcc_Volume0p1
brw-rw---- 1 root disk 252,   2 Aug 25 14:17 isw_bifaeaafcc_Volume0p2
brw-rw---- 1 root disk 252,   3 Aug 25 14:17 isw_bifaeaafcc_Volume0p5
ubuntu@ubuntu:~$

I eventually selected "Continue without a bootloader." in hopes that I can figure out how to install GRUB post-install.
Any ideas on getting GRUB installed, so this system will boot? Please let me know if you need any further details or system specs.
EDIT: Here is the output from /var/log/syslog during the failure
Aug 25 23:03:50 ubuntu grub-installer: info: architecture: amd64/generic
Aug 25 23:03:50 ubuntu grub-installer: info: Mounting /proc into /target
Aug 25 23:03:50 ubuntu ubiquity: grub-probe: error: failed to get canonical path of /dev/mapper/isw_bifaeaafcc_Volume0p1.
Aug 25 23:03:51 ubuntu ubiquity: grub-probe: error:
Aug 25 23:03:51 ubuntu ubiquity:
Aug 25 23:03:51 ubuntu ubiquity: failed to get canonical path of /dev/mapper/isw_bifaeaafcc_Volume0p1
Aug 25 23:03:51 ubuntu ubiquity: .
Aug 25 23:03:51 ubuntu ubiquity: File descriptor 3 (pipe:[140038]) leaked on lvdisplay invocation.
Aug 25 23:03:51 ubuntu ubiquity:  Parent PID 17588: /bin/sh
Aug 25 23:03:51 ubuntu ubiquity:
Aug 25 23:03:51 ubuntu ubiquity: Volume group "isw_bifaeaafcc_Volume0p1" not found
Aug 25 23:03:51 ubuntu ubiquity:
Aug 25 23:03:51 ubuntu ubiquity:
Aug 25 23:03:51 ubuntu ubiquity: Skipping volume group isw_bifaeaafcc_Volume0p1
Aug 25 23:03:51 ubuntu ubiquity:
Aug 25 23:03:51 ubuntu grub-installer: info: Identified partition label for /dev/mapper/isw_bifaeaafcc_Volume0p1: loop
Aug 25 23:03:52 ubuntu grub-installer: dpkg: warning: ignoring request to remove grub which isn't installed
Aug 25 23:03:52 ubuntu grub-installer: dpGkg: warning: ignoring request to remove grub-legacy which isn't installed
Aug 25 23:03:52 ubuntu grub-installer: dpkg: warning: ignoring request to remove grub-efi which isn't installed
Aug 25 23:03:52 ubuntu grub-installer: dpkg: warning: ignoring request to remove grub-efi-amd64-bin which isn't installed
Aug 25 23:03:52 ubuntu grub-installer: dpkg: warning: ignoring request to remove grub-efi-amd64 which isn't installed
Aug 25 23:03:52 ubuntu grub-installer: dpkg: warning: ignoring request to remove grub-efi-amd64-signed which isn't installed
Aug 25 23:03:52 ubuntu grub-installer: dpkg: warning: ignoring request to remove grub-efi-ia32-bin which isn't installed
Aug 25 23:03:52 ubuntu grub-installer: dpkg: warning: ignoring request to remove grub-efi-ia32 which isn't installed
Aug 25 23:03:53 ubuntu grub-installer: info: Installing grub on '/dev/mapper/isw_bifaeaafcc_Volume0p1'
Aug 25 23:03:53 ubuntu grub-installer: info: grub-install does not support --no-floppy
Aug 25 23:03:53 ubuntu grub-installer: info: Running chroot /target grub-install  --force "/dev/mapper/isw_bifaeaafcc_Volume0p1"
Aug 25 23:03:54 ubuntu grub-installer: Path `/boot/grub' is not readable by GRUB on boot. Installation is impossible. Aborting.
Aug 25 23:03:54 ubuntu grub-installer: error: Running 'grub-install  --force "/dev/mapper/isw_bifaeaafcc_Volume0p1"' failed.

Comment: What does `sudo grub-probe -t fs -vvd /dev/mapper/isw_bifaeaafcc_Volume0p1` show?
Answer: This is a fake raid controller.  Unless you must dual boot with Windows, it is best to avoid their use and stick with plain old Linux software raid, which you can set up using the alternate installer after destroying the fake raid array in the bios utility.
If you must use the fake raid, to troubleshoot the failure further you will need to check /var/log/syslog for more detailed error messages on why grub-install failed.
Comment: I would like to just use the fake raid, in case I ever need to mount the filesystem from a bootable USB stick. I edited my post with log output.

Bootloader install failed - RAID


How to set length in stream without truncated?

Question: Its stated that by using Stream.SetLength the stream is truncated.
source
I need to set the length of the stream without truncation.
Any build-in possibilities?
UPDATE:
I need to read a subset of a file.
the file format: size + data(string format).
Im using StreamReader
UPDATE2:
Im also veryintrested in using ReadLine from the StreamReader class.
Comment: Then what do you expect to happen?
Comment: You missed the key part of the sentence, "*If the specified value is less than the current length of the stream*, the stream is truncated." If you set the length to less then the current length, what else is it supposed to do?
Comment: what overall problem are you trying to solve?
Comment: The only way to not truncate the stream is to make it longer, and then where does the extra data come from? If you make it shorter, again, where does the data go? Kind of like wanting to cut your legs off at the knees, but you don't want to be any shorter...
Comment: So you are only trying to read a part of the file? You can do this without `SetLength `.
Comment: @JeffMercado I need to read a subset of the file. the length is less. I don't want to change the file. just read
Comment: Take a look at [this question](http://stackoverflow.com/questions/7514101/how-do-i-read-exactly-n-bytes-from-a-stream)
Comment: Possible duplicate of [How to expose a sub section of my stream to a user](https://stackoverflow.com/q/6949441)
Answer:
I need to read a subset of a file

OK, so you do not want to set the length actually. You want to read a particular length.
Write yourself a Stream subclass that reads only a certain amount of bytes. Then, you can wrap the file stream with that class and StreamReader will just work.
Or, if buffering the data is OK, you do this:
var limitedStream = new MemoryStream(new BinaryReader(myStream).ReadBytes(length));

Answer: I agree with @usr's answer about creating a subclass Stream wrapper.  Here's a sample implementation which forwards all calls to the "inner" stream, but gets involved during reading or when asked for Length.  Anything involving writing is unsupported with this implementation.
I haven't fully tested this implementation and I welcome edits from the community:
sealed class StreamReadLimitLengthWrapper : Stream
{
    readonly Stream m_innerStream;
    readonly long m_endPosition;

    public StreamReadLimitLengthWrapper(Stream innerStream, int size)
    {
        if (innerStream == null) throw new ArgumentNullException("innerStream");
        if (size < 0) throw new ArgumentOutOfRangeException("size");

        m_innerStream = innerStream;
        m_endPosition = m_innerStream.Position + size;
    }

    public override bool CanRead
    {
        get { return m_innerStream.CanRead; }
    }

    public override bool CanSeek
    {
        get { return m_innerStream.CanSeek; }
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void Flush()
    {
        m_innerStream.Flush();
    }

    public override long Length
    {
        get { return m_endPosition; }
    }

    public override long Position
    {
        get
        {
            return m_innerStream.Position;
        }
        set
        {
            m_innerStream.Position = value;
        }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        count = GetAllowedCount(count);
        return m_innerStream.Read(buffer, offset, count);
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return m_innerStream.Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        throw new NotSupportedException();
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        throw new NotSupportedException();
    }

    public override bool CanTimeout { get { return m_innerStream.CanTimeout; } }

    public override int ReadTimeout
    {
        get
        {
            return m_innerStream.ReadTimeout;
        }
        set
        {
            m_innerStream.ReadTimeout = value;
        }
    }

    public override int WriteTimeout
    {
        get
        {
            return m_innerStream.ReadTimeout;
        }
        set
        {
            m_innerStream.ReadTimeout = value;
        }
    }

    public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
    {
        count = GetAllowedCount(count);
        return m_innerStream.BeginRead(buffer, offset, count, callback, state);
    }

    public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
    {
        throw new NotSupportedException();
    }

    public override void Close()
    {
        // Since this wrapper does not own the underlying stream, we do not want it to close the underlying stream
    }

    public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
    {
        return m_innerStream.CopyToAsync(destination, bufferSize, cancellationToken);
    }

    public override int EndRead(IAsyncResult asyncResult)
    {
        return m_innerStream.EndRead(asyncResult);
    }

    public override Task FlushAsync(CancellationToken cancellationToken)
    {
        return m_innerStream.FlushAsync(cancellationToken);
    }

    public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
    {
        count = GetAllowedCount(count);
        return m_innerStream.ReadAsync(buffer, offset, count, cancellationToken);
    }

    public override int ReadByte()
    {
        int count = GetAllowedCount(1);
        if (count == 0)
            return -1;

        return m_innerStream.ReadByte();
    }

    public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
    {
        throw new NotSupportedException();
    }

    public override void WriteByte(byte value)
    {
        throw new NotSupportedException();
    }

    private int GetAllowedCount(int count)
    {
        long pos = m_innerStream.Position;
        long maxCount = m_endPosition - pos;
        if (count > maxCount)
            count = (int)maxCount;
        return count;
    }
}

Answer: You're asking the wrong question here.  The Length of a stream is the known amount of bytes are available to be read from the stream.  Setting the length of the stream alters the source to ensure it uses the length specified.

Since you merely want to limit how much you read from the stream, just read as much as you need and close it.  You don't need to do anything else.
Since it appears you're trying to read a text file, just go with the normal file reading patterns and come up with an appropriate stop condition.  You won't even need a stream, the File class provides simple ways to read the lines of a file.
foreach (var line in File.ReadLines(path))
{
    // do something with line...
    if (line == "end of interesting section")
        break;
}


How to swap the 0 and 1 values for each other in a pandas data frame?

Question: I am working with a pandas dataframe that has a column of all 0's and 1's and I am trying to switch each of the values (ie all of the 0's become 1's and all of the 1's become 0's). Is there an easy way to do this?
Answer: Use replace:
df = df.replace({0:1, 1:0})

Or faster numpy.logical_xor:
df = np.logical_xor(df,1).astype(int)

Or more faster:
df = pd.DataFrame(np.logical_xor(df.values,1).astype(int),columns=df.columns, index=df.index)

Sample:
np.random.seed(12)
df = pd.DataFrame(np.random.choice([0,1], size=[10,3]))
print (df)
   0  1  2
0  1  1  0
1  1  1  0
2  1  1  0
3  0  0  1
4  0  1  1
5  1  0  1
6  0  0  0
7  1  0  0
8  1  0  1
9  1  0  0

df = df.replace({0:1, 1:0})
print (df)
   0  1  2
0  0  0  1
1  0  0  1
2  0  0  1
3  1  1  0
4  1  0  0
5  0  1  0
6  1  1  1
7  0  1  1
8  0  1  0
9  0  1  1

Another solution:
df = (~df.astype(bool)).astype(int)
print (df)
   0  1  2
0  0  0  1
1  0  0  1
2  0  0  1
3  1  1  0
4  1  0  0
5  0  1  0
6  1  1  1
7  0  1  1
8  0  1  0
9  0  1  1

Timings:
np.random.seed(12)
df = pd.DataFrame(np.random.choice([0,1], size=[10000,10000]))
print (df)

In [69]: %timeit (np.logical_xor(df,1).astype(int))
1 loop, best of 3: 1.42 s per loop

In [70]: %timeit (df ^ 1)
1 loop, best of 3: 2.53 s per loop

In [71]: %timeit ((~df.astype(bool)).astype(int))
1 loop, best of 3: 1.81 s per loop

In [72]: %timeit (df.replace({0:1, 1:0}))
1 loop, best of 3: 5.08 s per loop

In [73]: %timeit pd.DataFrame(np.logical_xor(df.values,1).astype(int), columns=df.columns, index=df.index)
1 loop, best of 3: 350 ms per loop

Edit:
This should be faster:
import numexpr as ne
arr = df.values
df = pd.DataFrame(ne.evaluate('1 - arr'),columns=df.columns, index=df.index)

Comment: can you do that in one go? Isn't it going to create a conflict ? Just wondering how does the algorithm work? in parallel? creating views of the original df?
Comment: @DiegoAgher - yes, one go works perfectly. I think no conflict. And how it works it is hard question for me...
Answer: One easy way would be -
df[:] = 1-df.values

For performance, we might want to work with underlying array data, for a modified version like so -
a = df.values
a[:] = 1-a

Sample run -
In [43]: df
Out[43]:
   0  1  2
0  0  0  1
1  0  0  1
2  0  0  1
3  1  1  0
4  1  0  0

In [44]: df[:] = 1-df.values

In [45]: df
Out[45]:
   0  1  2
0  1  1  0
1  1  1  0
2  1  1  0
3  0  0  1
4  0  1  1

Using @jezrael's timings setup with the best solution from that setup for comparison against the one proposed in this post -
In [46]: np.random.seed(12)
    ...: df = pd.DataFrame(np.random.choice([0,1], size=[10000,10000]))
    ...:

# Proposed in this post
In [47]: def swap_0_1(df):
    ...:     a = df.values
    ...:     a[:] = 1-a
    ...:

In [48]: %timeit pd.DataFrame(np.logical_xor(df.values,1).astype(int), columns=df.columns, index=df.index)
10 loops, best of 3: 218 ms per loop

In [49]: %timeit swap_0_1(df)
10 loops, best of 3: 198 ms per loop

Or even better to use the negation of the boolean version of input array data -
In [60]: def swap_0_1_bool(df):
    ...:     a = df.values
    ...:     a[:] = ~a.astype(bool)
    ...:

In [63]: %timeit swap_0_1_bool(df)
10 loops, best of 3: 179 ms per loop

Comment: Might even try `a[:] = np.logical_not(a)`, which doesn't require the `astype` reassignment
Answer: If your dataframe consists only of 1s an 0s, you can use XOR operator df ^ 1
In [19]: import pandas as pd

In [20]: df = pd.DataFrame({"a": [1,0,1], "b": [0,1,1]})

In [21]: df
Out[21]:
   a  b
0  1  0
1  0  1
2  1  1

In [22]: df ^ 1
Out[22]:
   a  b
0  0  1
1  1  0
2  0  0


Does runat="server" img scr=~/img/ex1.png" work in ASP or only ASP.net | Relative to Absolute path in include

Question: so: I've read 8+ stackover flows on relative to absolute.   A few sites and went to MS page on this...
however when I've tried and had not success.  Then I researched more and wonder solutions I read about are not part of .asp  and just asp.net? (yup, I'm a novice / newbie / ignorant)
The situation and my aspiration:
I have a site that was initial just a few products and pages of text / images(~10),  the number of products has doubled and I want to migrate to a tree structure for SEO / personal sanity / organization.
The original builder of site used  This was great on single layer set up, as it is now.
i.e.  example.com/page1.asp
Now I'm putting reviews (lengthy multiple page product reports), adding multiple products, etc on different depths in a tree.   So while I can accurately connect to a relative include on
example.com/products/super-widgets/widget/reviews/index.asp

the sources inside the include break on as they are relative.
As I have attempted to research, stated above, I have tried to find a "best practice" for converting the relative to absolute for the information in the /inc-footer.asp file.  I have consider putting an absolute path for agent to get global images
.
However I believe some say this is a poor choice for coding, I think it is because of speed of experience of clients / web visitors, but I don't know this.
So I have attempted a few trials and errors based on my earlier research with no avail....
inside the include file.asp  I have (and failed to succeed with):
from this sites examples (link)  I tried:
Page 2

and from  MS (link) I tried:
<asp:image runat="server" ImageUrl="~/images/tab-rightside.png" alt="design object" width="21" height="20" class="tab-img" /> Page 1

while writing this I referenced some of recommendations and tried & failed with ( link )
<img  src="<%= VirtualPathUtility.ToAbsolute("~/images/tab-rightside.png") %>" class="tab-img" width="22" height="20" />Page 2

Then I thought I need to modify above example and tried
<img  src="<%= VirtualPathUtility.ToAbsolute("~/images/") %>tab-rightside.png" class="tab-img" width="22" height="20" />Page 2

Again with cross reference in writing this (link), I considered a   but the include file has no
Only success I have had success with absolute path to http://
Page 2

Presently, the site is hosted on Windows Server 2003 Microsoft-IIS/6.0
All of the page files are .asp
I use Google chrome to define success in testing above examples.  As I experience if it doesn't' work in chrome it's not worth testing in other browsers.
Thanks.
Casey Burnett
Comment: See this answer to a recent asp-classic question: http://stackoverflow.com/questions/7963635/resolveurl-url-content-equivalent-in-classic-asp/7972602#7972602
Answer: <img src='<%= Page.ResolveUrl("~/images/tab-rightside.png") %>'
    class="tab-img" width="22" height="20" />
    Page 2

Comment: Thank you.unfortunately I received an error in testing with this too.
Comment: Thank you. Unfortunately I received an error in testing with this too.

Just to clarify there are just four lines of code pointing to separate page each looks like this (plus id= and class= which I edited out of fit limits of this comment box]  in the Include file.asp
`Page 1`
Answer: Runat = Server won't work for classic ASP. Classic ASP and ASP.net are not interchangeable. They work completely differently.
Regardless of framework I've found Root relative paths work well when your development site is not using local host.
** Root Relative example**

Here is an article on the different path types:
http://brugbart.com/Articles/paths
If you can I would use Root Relative paths, if not use absolute URLs. The only penalty for Absolute URLs the download overhead of a few extra characters in the URL.


C# ListView simplified

Question: For example:
listView1.Items[0].SubItems[1]

Shows like:
ListViewItem{ OBJECTNAMELOL }

I know i can just replace the ListViewItem{ }
But is there an easier way which shows it just without it?
Comment: Please mark the answer as accepted (check box) if you found it useful.
Answer: Terribly asked question...but I think what you're looking for is:
listView1.Items[0].SubItems[1].Text;

Comment: Thanks mate i was looking for this soo much :D

C# ListView simplified


Abs.convergence of a complex-valued sum

Question: Im trying to figure out if this sum $$\sum_{n=1}^\infty \frac{e^{ik}}{n^{2}} $$
is absolutely convergent. There is a step in my solution that Im not 100% sure of.
$$\sum_{n=1}^\infty \left| \frac{e^{i*n}}{n^{2}}\right| =\sum_{n=1}^\infty \left| \frac{cos{(n)}+isin{(n)}}{n^{2}}\right| \le \sum_{n=1}^\infty \frac{\sqrt{(cos{(n))^{2}}+(sin{(n)}})^{2}}{n^{2}} = \sum_{n=1}^\infty \frac{1}{n^{2}} $$
Hence it is absolutely convergent also. But Im not sure if the inequality that I used can be used. Can one say the absolute value of a complex number is less than the distance?
Comment: For any real number a, we have $|e^{ia}|$ = 1, because $\sin^2 a + \cos^2 a = 1$.
Comment: Actually, you can write $=$ instead of $\le$.
Comment: Okay, so it is valid to write = ? And does my solution look ok?
Comment: Yes, it is right.
Answer: Since the series is in the form of
$$\sum_{n=1}^\infty \frac{a_n}{n^s},$$
where $s=2$ and $a_n=e^{in}$, it converges absolutely, since $a_n$ is bounded and Re$(s)=2>1$. This comes from a more general result - https://en.wikipedia.org/wiki/Dirichlet_series
Answer: It's right except that it is not "$\sqrt{(cos{(n))^{2}}+(sin{(n)}})^{2}$" but "$\sqrt{(\cos{(n))^{2}}+(\sin{(n)})^{2}}$" (the line of the square-root. In fact, $|z|$ is defined either as the Euclidean distance of $z$ from the origin in the complex plane, or as $\sqrt{zz^*}$ (where $z^*$ is the conjugate of $z$). In any case, $|x+yi| = \sqrt{x^2+y^2}$ for any reals $x,y$.

Abs.convergence of a complex-valued sum


cross compiling e2fsprogs for arm

Question: I'm trying to cross compile extundelete linux package for arm. I want to run it in my Android device.
When I try to run ./configure it says Can't find ext2fs library. I've found the problem is extundelete requires ext2fs library. This library is into e2fsprogs package.
So I've tried to cross compile e2fsprogs. Static compilation is not posible because it seems e2fsprogrs needs --enable-elf-shlibs to run make command correctly. I've built e2fsprogrs with command:
"./configure --host=arm-none-linux-gnueabi" CC=arm-none-linux-gnueabi-gcc --enable-elf-shlibs && make && make install && make install-libs.

It runs correctly and I get cross compiled e2fsprogs. Problem is this is getting libraries compiled dinamically and I dont how to pass it to extundelete cross compilation and then what archives I have to copy to my android device.
I think it's posible to need to link the .so .a files I'm getting in e2fsprogs cross compilation but I dont understand exactly how to make this.
In one of my adventures cross compiling extundelete I got binary arm extundelete file but this was compiled dynamically linked and uses shared libraries. I tried to copy extundelete binary and .so files from e2fsprogs to my Android device but it doesnt run correctly.
Anyone can help me to understand the wonderful world of dynamic cross compilation?
Answer: ../configure --host=arm-none-linux-gnueabi --with-cc=arm-none-linux-gnueabi-gcc --with-linker=arm-none-linux-gnueabi-ld --with-ldopts=--static --prefix=/tmp/target
Answer: Be aware that newer version (1.43 for example) doesn't use --with-cc or --with-ldopts anymore. --with-linker also isn't used any more.
Use CC= and LDFLAGS= instead.
example:
./configure --host=arm-none-linux-gnueabi CC=arm-none-linux-gnueabi-gcc LDFLAGS=--static --prefox=/target/dirctory

Cheers!

cross compiling e2fsprogs for arm


How to mock a buffered file handle for unit testing

Question: I want this to work correctly without buffering:
$ tail -f logfile | python parser.py

Previously I used:
for line in sys.stdin:
    parse(line)

But now I have implemented https://stackoverflow.com/a/18235323/604515 :
while True:
    line = sys.stdin.readline()
    if not line: break # EOF
    parse(line)

Question: I cannot think of a way to unit test my change without sleep(). How do I simulate a buffered filehandle? I can easily mock stdin, but then it will never buffer, so that won't prove anything.
Edit: I've looked at StringIO but that doesn't seem to have the same buffering behaviour as a normal filehandle.
Comment: You can make your own custom class and set sys.stdout = class
Comment: do you want it buffered or unbuffered?
Comment: I want a unit test that verifies that it never buffers, in case someone changes the code back to ```for line in stdin```
Comment: what version of python?
Comment: 2.7, unfortunately. Thanks!
Comment: On a side note `for line in iter(sys.stdin.readline,"")` will do the same as your while loop
Comment: If someone changes the line to `for line in sys.stdin` without doing some magic  with subprocess or pseudo terminals then it is always going to buffer output.
Answer: First of all consider to parameterize your function by pass the stream. You can use default to take sys.stdout. Your method become something like that:
def my_reader(feed=sys.stdin):
    while True:
        line = feed.readline()
        if not line:
            break
        parse(line)

Now you can mock your feed by mock framework and patch your parse method. Unfortunately you cannot patch directly sys.stdin.readline() method because is a read only attribute.
Now the steps in your test are: create a mock of stdin in setUp() and in your tests configure the it and do your test. Follow an example.
#If you trust parse and you have no other way to test parse instead check its calls you need to patch it
@patch(__name__ + ".parse")
class MyTestCase(unittest.TestCase):
    def setUp(self):
        #Our 'feed'
        self.mock_feed = MagicMock()
        #readline mock
        self.mock_readline = self.mock_feed.readline

    def test_my_reader_empty(self, mock_parse):
        #setting up an empty stream
        self.mock_readline.return_value = ""
        my_reader(self.mock_feed)
        #Our test is that parse was never called
        self.assertFalse(mock_parse.called)

    def test_my_reader_some_lines(self, mock_parse):
        #setting up lines sequence and EOF
        self.mock_readline.side_effect = ["hi,\n", "I'm your standard input\n", "cheers", ""]
        my_reader(self.mock_feed)
        #Call parse with every lines
        mock_parse.assert_has_calls([call("hi,\n"), call("I'm your standard input\n"), call("cheers")])

Just a note: I patched the class instead patching all method as documented here.


cucumber + java :Passing paramater returned in step1 to step2

Question: I am new to cucumber. We have below use case for UI automation through cucumber.
Please consider below example.
in TestNg,
<code>@Test {"formname"}
public void createAndSearchForm(String formName)
{
    //In below  step, it create form by name (formName-timestamp) and return   the formname. e.g. it create form and return "formname-06042016184426"
    // In this method we create form name by appending time stamp to formname passed to method. Since application didn't accept same name we need to append timestamp
    //   to formname.
    String newFormName=createForm(formName);

    // In below method we pass above newFormName and verify whether form is created or not by searching form name.
    asserTrue(searchCreatedForm(newFormName));

}
</code>
Now we are moving to cucumber and we need to accomplish above example in cucumber.
Feature: Forms.
Scenario: Login to application
Given Create form with name "formname"
Then Search "formname"
Issue that we are facing ->
The formname that is getting returned in step1, we don't know how to pass it to step2 .
Within scenario, We need to pass this form name to different step definition which is implemented in various classes.
I tried to search over the net however didn't  find anything specific to our need.
It will be great help if anyone could give us some pointers/suggestion.
Comment: Simple answer is class variables.
Answer: EDIT
When I need to share variables across multiple step files I create a superclass that defines them.  Each of my step files extends that superclass giving them all access to that variable (or variables).
As stated in the comments, be careful about making the class variables static as you might leak state.  I set the values of any static variables to a value (e.g. null) in the class constructor so that the value is reset for each scenario.
--End edit--
<code>public class YourStepDefinitions {

    private String interStepParameter;

    @Given("^Some first step$")
    public void first_step() {

        interStepParameter = "foo";

    }

    @Then ("^A second step$")
    public void second_step() throws Throwable {
        if (interStepParameter.equals("foo") {
           // Do something
        }
    }
}
</code>
Comment: Thanks Mike.
We thought of this approach however we need to pass this variable to many step definition that are implemented in different classes.
We have many test cases which have same scenario.So we thought it will not be a good idea to implement this way.
Comment: Your driver is shared across classes.  Assuming that you are not running parallel, create another static variable who's handle gets passed around, just like driver.
Comment: Please note that you might accidentally leak state between scenarios this way (if the variable is set during a test and not reset before the next test). This will cause your tests to be flaky and unreliable. And also this will only work as long as all of the steps that need this variable are in the same file. Consider using Dependency Injection to manage the fields & objects you'd like to share between different steps. the DI container can then manager their creation and (if configured correctly) their destruction.
Comment: @Marit Oh, you mean if static.  Just add a class constructor that sets a static interStepParameter to null.


How to test if a variable holds a lambda-function?

Question: I'm building a function where the main argument can be various things, e.g. a formula, a function or a lambda-function and it has to handle things accordingly.
library(rlang)
my_func=function(x){
    if(is_function(x))
        return("X is a function")
    else if(is_lambda(x))
        return("X is a lambda")
    else if(is_formula(x))
        return("X is a formula")
    else
        return("X is something else")
}
my_func(x=is.numeric) #function
my_func(x=A~B) #formula
my_func(x=~is.numeric(.x)) #formula too :'(
my_func(x="foo") #something else

As you can see, rlang::is_lambda did not recognize the lambda-function. In the help file example, they use as_function first (is_lambda(as_function(x))), but this throws a conversion error when x is not a lambda-function-formula. I'd rather not use trycatch if possible, as I find it sometimes leads to unreadable code and hidden bugs.
How can I test this case simply?
Comment: @Brian As I wrote, `rlang::as_function` throws a conversion error when `x` is not a lambda-function-formula. This would make the second example fail with an error.
Comment: Oh, I misread that, you're right. I assumed you wanted it to exclude non-function-like arguments. How will your function work when passed a two-sided formula? Maybe this is something the downstream logic needs alteration for.
Answer: While Allan's answer works fine, I decided to stick to what rlang was doing.
If you take a look at View(rlang::as_function), you'll see that it aborts only if (length(x) > 2).
Therefore, my code can be written as:
library(rlang)
my_func=function(x){
  if(is_function(x))
    return("X is a function")
  else if(is_formula(x)){
    if(length(x) > 2)
      return("X is a formula")
    else if(is_lambda(as_function(x)))
      return("X is a lambda")
  }
  return("X is something else")
}

This will return the expected output every time.
Answer: You could deparse the formula and test whether its first character is a tilde. This will mean any formula without a left hand side that could be coerced to a lambda will be recognised as such, but I'm guessing that is the desired behaviour anyway.
library(rlang)

my_func=function(x){
    if(is_function(x))
        return("X is a function")
    else if(is_formula(x)){
        if(substr(deparse(x), 1, 1) ==  "~"){
            if(is_lambda(as_function(x)))
                return("X is a lambda")
        }
        return("X is a formula")
    }
    else
        return("X is something else")
}

my_func(x=is.numeric)
#> [1] "X is a function"
my_func(x=A~B)
#> [1] "X is a formula"
my_func(x=~is.numeric(.x))
#> [1] "X is a lambda"
my_func(x="foo")
#> [1] "X is something else"

Created on 2020-03-16 by the reprex package (v0.3.0)
Comment: Finally, I found out that `as_function` check if `length(x) > 2` before returning an error. I don't know if this covers all eventualities but I guess I will not do better than `rlang`. But your solution works as well.


Unable to open Sagemath Jupyter in Firefox (after upgrade to 22.04)

Question: I am trying to open Sagemath through Jupyter. I am getting the following message in Firefox:
Access to the file was denied

The file at /home/tw/.local/share/jupyter/runtime/nbserver-61611-open.html is not readable.

It may have been removed, moved, or file permissions may be preventing access.

Recently, I migrated to Ubuntu 22.04 (from 20.04). Under Ubuntu 20.04, Juypter worked finely. Also the file permissions are fine and all set to "rw" for all users. I have read somewhere that Firefox does not open hidden files anymore. I tried to change advanced settings in Firefox, without success. I could not even find the relevant advanced setting option. I cannot open the above html-file from chrome either. Reinstalling Sagemath was also not helpful.
Answer: In 22.04, Ubuntu ships Firefox as a snap. The snap version of Firefox has strict file permissions for the sake of security, but often it compromises the user friendliness. The solution is to replace the snap version of firefox with the apt version.
Add the Firefox PPA maintained by Mozilla team
sudo add-apt-repository ppa:mozillateam/ppa

Copy and paste the following code in a terminal (don't copy-paste line by line) to prioritize the apt version of firefox with the snap version.
echo '
Package: *
Pin: release o=LP-PPA-mozillateam
Pin-Priority: 1001
' | sudo tee /etc/apt/preferences.d/mozilla-firefox

Remove the snap version of firefox
sudo snap remove firefox

Install Firefox with apt.
sudo apt install firefox

This version of Firefox should update when you update the system with the update manager (but it does not automatically upgrade in the background like the snap version).
Source: OMG Ubuntu
Comment: Worked, fantastic!
Comment: After each reboot, the error message reappears, seemingly the snap installation reappears. What do I have to do to make this fix permanent? I have actually run the above script after booting each time.
Comment: Interesting. Are you sure that Firefox reappears in snap? Did you check `snapd list`? If it does reappear, consider removing snaps altogether.
Comment: I switched to Jupyter Notebook, using the sagemath backend. Works finely. I would like to avoid removing snap.
Answer: Instead of messing with Firefox, you can access SageMath through http://localhost:8888 instead.
Comment: @ArturMeinild The question mentions using Firefox, and the other answer suggests removing the snap version of Firefox and replacing it with the APT version. My answer offers a different solution that lets you keep the snap.
Comment: How does this relate to using or not using Firefox?


Chrome - Password/Credit Card input within an iFrame

Question: I am using an iFrame which is hosted on another domain that has an SSL certificate. On the page where the iFrame is used I get 'Not Secure' message next to the domain in Google Chrome and the following warning in the console log:

This page includes a password or credit card input in a non-secure
  context. A warning has been added to the URL bar. For more
  information.

Note: The iFrame loads an online booking portal which includes both login details and credit card information.
In order to prevent this message from showing, would I need to purchase an SSL certificate?
I'm asking because I don't want to order one if it doesn't resolve the issue, there is a way around this without having to get a SSL certificate or if it's just not worth getting one for this situation.
Your thoughts would be highly appreciated.
Thanks in advance.
Answer:
To help users browse the web safely, Chrome indicates connection
  security with an icon in the address bar. Historically, Chrome has not
  explicitly labelled HTTP connections as non-secure. Beginning in
  January 2017 (Chrome 56), we'll mark HTTP pages that collect passwords
  or credit cards as non-secure, as part of a long-term plan to mark all
  HTTP sites as non-secure.

It applies to all sites that are not https.
Do check for any website which have login information and doesn't have https, you can view the username and password in the chrome developer window.
security page documentation


can we use advisory recordlocking without usleep() to alternate between the parent and the child?

Question: I am learning "Advanced Programming in Unix Environment", and have a problem with exercise no.17 in chapter 15.
The exercise ask reader to "use advisory recordlocking to alternate between the parent and the child".
And i found that it can not be done without using usleep(). There is nothing to stop kernel continually scheduling parent after unlocking the record, and vice versa.
Does anyone has the answer? Thanks in advance.
HERE is my code:
ex17.c
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/wait.h>
#include "ch14/lock.h"

#define NLOOPS 1000
#define SIZE sizeof(long)               /* size of shared memory area */

static int update(long *ptr)
{
    return (*ptr)++;                    /* return value before increment */
}

int main(int argc, char* argv[])
{
    int fd, counter, lockFd;
    pid_t pid;
    void *area;

    if((fd = open("/dev/zero", O_RDWR)) < 0)
    {
        perror("open error");
        exit(1);
    }

    if((area = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
    {
        perror("mmap error");
        exit(1);
    }

    close(fd);                          /*can close /dev/zero now that it's mapped */

    if((lockFd = open("ex17.lock", O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0)
    {
        perror("open error");
        exit(-1);
    }

    /* insure parent runs first */
    writew_lock(lockFd, 0, SEEK_SET, 1);                        /* first byte in the lock file represent parent */
    writew_lock(lockFd, 1, SEEK_SET, 1);                        /* second byte in the lock file represent child */

    if((pid = fork()) < 0)
    {
        perror("fork error");
        exit(1);
    }else if(pid > 0){                  /* parent */
        for(int i = 0; i<NLOOPS; i+=2)
        {
            writew_lock(lockFd, 0, SEEK_SET, 1);
            readw_lock(lockFd, 1, SEEK_SET, 1);

            if((counter = update((long*)area)) != i)
            {
                fprintf(stderr, "parent: expected %d, got %d\n", i, counter);
                exit(1);
            }else{
                printf("%s got %d\n", "parent", counter);
            }
            un_lock(lockFd, 1, SEEK_SET, 1);
            usleep(1000);  /*if without this line, there is nothing to stop kernel continually scheduling parent.*/
            readw_lock(lockFd, 0, SEEK_SET, 1);
        }
        waitpid(pid, NULL, 0);
    } else {
        for(int i=1; i<NLOOPS+1; i+=2)
        {
            writew_lock(lockFd, 1, SEEK_SET, 1);
            readw_lock(lockFd, 0, SEEK_SET, 1);

            if((counter = update((long*)area)) != i)
            {
                fprintf(stderr, "child: expected %d, got %d\n", i, counter);
                exit(1);
            }else{
                printf("%s got %d\n", "child", counter);
            }
            un_lock(lockFd, 0, SEEK_SET, 1);
            usleep(1000);
            readw_lock(lockFd, 1, SEEK_SET, 1);
        }
    }
    exit(0);
}

ch14/lock.h
#ifndef CH14_LOCK_H_INCLUDED
#define CH14_LOCK_H_INCLUDED

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int lock_reg(int, int, int, off_t, int, off_t);
pid_t lock_test(int, int, off_t, int, off_t);

#define read_lock(fd, offset, whence, len) lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define readw_lock(fd, offset, whence, len) lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define writew_lock(fd, offset, whence, len) lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
#define un_lock(fd, offset, whence, len) lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))

#define is_read_lockable(fd, offset, whence, len) (lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
#define is_write_lockable(fd, offset, whence, len) (lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)

#endif // CH14_LOCK_H_INCLUDED

ch14/lock.c
#include "lock.h"

int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
    struct flock lock;

    lock.l_type = type;                            /* F_RDLCK, F_WRLCK, F_UNLCK */
    lock.l_whence = whence;                    /* byte offset, relative to l_whence */
    lock.l_start = offset;                        /* byte offset, relative to l_whence */
    lock.l_len = len;                                /* #bytes (0 means to EOF) */

    return fcntl(fd, cmd, &lock);
}

pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len)
{
    struct flock lock;

    lock.l_type = type;                            /* F_RDLCK OR F_WRLCK */
    lock.l_start = offset;                        /* byte offset, relative to l_whence */
    lock.l_whence = whence;                     /* SEEK_SET, SEEK_CUR, SEEK_END */
    lock.l_len = len;                                /* #bytes (0 means to EOF) */

    if(fcntl(fd, F_GETLK, &lock) < 0)
    {
        perror("fcntl error");
        exit(-1);
    }

    if(lock.l_type == F_UNLCK)
    {
        return 0;                                    /* false, region isn't locked by another proc */
    }else{
        return lock.l_pid;                            /* true, return pid of lock owner */
    }
}

Comment: You're never checking the locks.  *Advisory* locks are not enforced, and right now you're ignoring the locks you've set; they serve no purpose.  Once you add the check, you'll probably need a (very short) time delay to prevent busy looping, since there are no blocking calls (most likely, you should incorporate that loop into a function, making it effectively a blocking call).
Comment: hi, @goldilocks. Thanks for answering. Advisory locks are enforced only if they were held by different processes. By "checking the locks" do you mean i should check the owner of the lock? Even if i add lock owner checking, that does not eliminate the need of usleep() calls in the code.
Comment: @goldilocks thanks, maybe your ways is the best solution for this. But don't understand why the author use this as a exercises for the book.
Comment: TBH while it's a challenging exercise, I don't think you'd be learning much that you'll actually apply later.  File locks are an  old concept with limited use value -- using them to synchronize processes, as here, is sort of silly.  That is better done using more direct forms of IPC, or multi-threading.  So it's nice to know about, but you don't need to spend a lot of time on it.  Anyway, I've added an answer explaining what I meant by checking the locks, etc.
Answer: Advisory lock implementations differ from one unix-like OS to another, but they usually are not mandatory by default, and this is the case with linux.  man fcntl contains a section for advisory locks and mandatory locks; from the beginning of the latter (where "above" refers to the former):

The above record locks may be  either  advisory  or  mandatory,  and  are  advisory  by default. Advisory locks are not enforced and are useful only between cooperating processes.

This means in order for the lock to be effective, each cooperating process must check to see if it can acquire one before it does so.  man fcntl says some things about this for the F_GETLK call:

F_GETLK (struct flock *)
On input to this call, lock describes a lock we would like to place on the file.  If the  lock could  be  placed, fcntl() does not actually place it, but returns F_UNLCK in the l_type field of lock and leaves the other fields of the structure unchanged.  If one or  more  incompatible
  locks  would  prevent  this lock being placed, then fcntl() returns details about one of these locks in the l_type, l_whence, l_start, and l_len fields of lock and sets l_pid to be the  PID of the process holding that lock.

So the struct you submit using F_GETLK may have some fields changed to indicate a result -- this is what you need to check.  If it succeeds, you can then call with F_SETLK to actually apply the lock.1  If it doesn't succeed, you need to wait until it does, and none of these calls are blocking, meaning they return immediately regardless of the situation.  This is where the necessity of a short sleep comes in, because if you simply loop around checking the lock over and over again, you will be busy looping a processor (maxing it out doing nothing).  However, if you throw in a 5 or 10 ms passive delay, the loop will be mostly doing nothing passively (i.e., without maxing out a processor).  This delay should be only after a failed check; if the check succeeds, immediately set the lock.
All that can go in a single function.  It does not take care of getting the processes to steadily alternate, but this is actually sort of a contrived criteria: if you wanted to accomplish that goal in "real life", you would not go about it this way.  However, it's not impossible; the first thing I would try would be to use a sleep at the beginning of the function which is several times longer than the one in the loop, so that a process which releases a lock and then goes to acquire it again will be delayed longer than a process which was concurrently trying to get it in the check/set loop.

1. Unfortunately, that implies a potential race condition whereby one process may set a lock in between another process's check and set calls -- a weakness of this system.


Find the form of the most powerful critical region of size $\alpha$.

Question: A single observation $x$ is used to test the null hypothesis $\theta=\theta_0$ against alternative hypothesis $\theta=\theta_1<\theta_0$ for a geometric distribution with parameter $\theta$. Use the Neyman-Pearson lemma to find the form most powerful critical region of size $\alpha$.
Initally, I started with $$L_0=\prod_{x=1}^n \theta_0(1-\theta_0)^{x-1} = \theta_0^n(1-\theta_0)^{-n+\sum_{x=1}^n x}$$ and $$L_1=\prod_{x=1}^n \theta_1 (1-\theta_1)^{x-1} = \theta_1^n(1-\theta_1)^{-n+\sum_{x=1}^nx}$$
Then, $\dfrac{L_0}{L_1}=\dfrac{(1-\theta_0)^{\sum_{x=1}^n x}}{(1-\theta_1)^{\sum_{x=1}^n x}}\leq K$ inside the critical region and $\geq K$ outside the critical region. However, this expression is very complex, so I wasn't sure how to simplify this any further.
I then thought since it says a single observation of $x$, can I just let $x=1$? So then I end up with $L_0=\theta_0$ and $L_1=\theta_1$. However, then I get the critical region is $\frac{\theta_0}{\theta_1}\leq k$, which I didn't think made sense becaue it doesn't depend on any variables.
Comment: Your reason for thinking that that result doesn't make sense is certainly correct. $\qquad$
Comment: I cannot answer your question (comment) if you delete it. https://math.stackexchange.com/questions/2372595/find-the-likelihood-ratio-statistic-lambda-for-a-normal-population?noredirect=1#comment4890428_2372595
Answer: The observation $x$ is a number in $0,1,2,\ldots $. The likelihood function is $$ \theta(1-\theta)^{x-1}$$ so the likelihood ratio is $$ \frac{\theta_0}{\theta_1}\left(\frac{1-\theta_0}{1-\theta_1}\right)^{x-1}$$ and depends on the data $x$. (And you're correct that it would be strange if it didn't.)
Comment: With the Neyman-Pearson lemma, is it always the case that $\frac{L_0}{L_1}\leq k$ inside the critical region and $\geq$ outside the critical region regardless of if $\theta_0>\theta_1$ or $\theta_1>\theta_0$?
Comment: @mmm Yes, iirc the lemma says that the critical region for a most powerful test at a given confidence level will have that form (where $k$ is determined by the desired confidence level). That said, often the calculation of the region splits into two cases, depending on what side of $\theta_0$ $\theta_1$ is on. (For instance here it's clear that what you'll get at the end of the day is a region of the form $x\theta_0$ and $x>C'$ in the case where $\theta_1<\theta_0$)
Answer: My first thought was that where you wrote $\displaystyle\prod_{x=1}^n \theta_0(1-\theta_0)^{x-1}$ and $\displaystyle \sum_{x=1}^n x,$ you need $\displaystyle \prod_{i=1}^n \theta_0(1-\theta_0)^{x_i-1}$ and $\displaystyle \sum_{i=1}^n x_i$ respectively, so multiplication and addition begin at $i=1,$ not at $x=1,$ and you have $x_i$ rather than $x.$

My second thought was that you wrote "a single observation", and that means $n=1,$ so your expressions are certainly more complicated than they need to be.

So you have $$ \frac{L_0}{L_1} = \frac{\theta_0(1-\theta_0)^{x-1}}{\theta_1(1-\theta_1)^{x-1}} = \left(\frac{1-\theta_0}{1-\theta_1}\right)^{x-1}. \tag 1 $$
Since $\theta_1 < \theta_0,$ the expression $(1)$ decreases as $x$ increases. Since small values of the ratio on the left in $(1)$ favor $H_1,$ that means large values of $x$ favor $H_1.$
To figure out how large is large enough, you need to solve
$$
\Pr(X>c\mid H_0) \le \alpha \text{ for } c.
$$
Comment: So, when trying to find the rejection region, one must determine whether $L_0/L_1$ increases or decreases with increasing value of x?
Comment: @mmm : Yes, but in some problems you might want some function of $x$ other than $x$ itself. For example, $x$ might be $(x_1,\ldots,x_n)$ and you use the sum or the product or the sum of squares or the maximum of these. Often $L_0/L_1$ is a complicated function of $x$ and you seek a simple function of $x$ that is a monotone (i.e. always increasing or always decreasing) function of $L_0/L_1.$ That is how you see that the t-test and the F-test are likelihood-ratio tests. $\qquad$


Add incrementing value to the column in postgresql

Question: I have a table which contains 69000 records. Now, how to add a new column(say index) which should contain the value 1 for 1st row, 2 for 2nd row ....etc.
I want the following result
   id      name      index
 --------------------------
 9796896   sandy       1
 796869    raj         2

Comment: You can add the new column as IDENTITY column.
Comment: Why have you tagged this with Oracle if you are using PostgreSQL? (Also 'index' seems like a poor choice name for a column... it wouldn't be legal in Oracle, unless quoted.)
Comment: Possible duplicate of [PostgreSQL Autoincrement](https://stackoverflow.com/questions/787722/postgresql-autoincrement)
Comment: Check this out https://www.tutorialspoint.com/postgresql/postgresql_using_autoincrement.htm#
Answer: Add the column and update it with something like that :
with cte as
(
   select *
       , rank() over (order by id desc) as ranker
   from test
)
update test
   set index = ranker
from cte
where cte.id = test.id;

Answer: Try this:
ALTER TABLE Tablename
    ADD COLUMN index int
        GENERATED BY DEFAULT AS IDENTITY;

Comment: generated columns ? Are they not available only for version 12 ?
Answer: you can use identity column of the oracle:
alter table your_Table add index_ number GENERATED by default as IDENTITY;

also, you can add the column and then assign value to it:
alter table your_Table add index_ number;

update your_Table tt
set tt.index_  =  (select rn from (
select row_number() over (order by id desc) rn
from your_Table t
) t where t.id = tt.id)

Cheers!!
Answer: This worked nicely for me running postgres 9.6:
ALTER TABLE test ADD COLUMN index BIGSERIAL ;

Short one liner and it keeps on auto-incrementing in future inserts.
Although I think the count started at zero.  I'll update answer after I checked.


Sample 5 times and sum, with replacement

Question: I am trying to sample with replacement with a slight twist. I want to sample a list, vector, etc... 5 times and sum that. And replace the 5 sampled values, and do this 1000 times.
x<- 1:1000
samp <- matrix(NA, ncol = 1, nrow = 1000)
for(i in length(x)){
samp[i,] <- sum(sample(x,5,replace = TRUE))}

What I cant figure out is why this loop isnt working
Answer: You missed 1: in front of length(x) (i.e. from 1 to length of x by 1). Your code should be like this:
x<- 1:1000
samp <- matrix(NA, ncol = 1, nrow = 1000)
for(i in 1:length(x)){
  samp[i,] <- sum(sample(x,5,replace = TRUE))}

Which works great:
> str(samp)
 int [1:1000, 1] 2715 2312 3180 1364 2851 2429 2888 2381 2772 2317 ...

Also, for-loops have a bad reputation of being slow in R so you might want to consider other ways of looping like using the C++ way with (for example) replicate like this:
Define the function:
myfunc <- function(x) {
  sum(sample(x,5,replace = TRUE))
}

And then use it like this:
x <- 1:1000
mymat <- matrix(replicate(1000, myfunc(x)), ncol=1)

> str(mymat)
 int [1:1000, 1] 2481 2236 2492 1759 1905 3243 2606 2624 3013 2309 ...

Comment: This works great! Also thanks for the function idea :)
Comment: Really glad I could be of help :)


Problem with Entity Framework extensions .BulkSaveChanges - Saving Guid

Question: I'm using EF extension to do BulkSaveChanges, and I'm using SQLite, everything works perfectly, fast saving, etc. The problem I'm having is that when saving Guids, it's as if BulkSaveChanges is converting the Guids to TEXT first before saving, but the data type is BLOB, then when retrieved from the database through DbContext, GUIDs are not recognizable, my theory is that because the data is converted and the fact that the data type is BLOB, EF, couldn't convert it back to Guids implicitly.
Moreover, when using the EF's .SaveChanges, everything is preserved, also retrieval is okay, but .SaveChanges is slow.
Any help will be greatly appreciated.
So here's the link to the extension: https://entityframework-extensions.net/bulk-savechanges
Here's some screenshots of data being saved with .SaveChanges vs. .BulkSaveChanges
Saved with .SaveChanges:

Saved with .BulkSaveChanges:
Answer: I was able to make things work and able to live with this situation considering:

That SQLite does not support GUIDs and it gives BLOB data type to data that it can't recognize.
That BulkSaveChanges converts or should I say corrects the data before it's persisted, in this case converts it to TEXT.

The problem however is that the data type remains BLOB, then when retrieved through EFCore, converting back to GUID implicitly, wouldn't work.
Upon analyzing the situation, I realized why can't I just define the data type of the GUIDs to TEXT? since BulkSaveChanges does a conversion to TEXT anyway. And in that way, EFCore would be able to see that the GUIDs are saved as TEXT and not BLOB so it can know how to convert it back to GUID implicitly. Turned out, it's possible!
Here's what I did, I added these lines when we build the model through Fluent API for entities that contain GUIDs:
entityTypeBuilder
    .Property(l => l.GUID)
    .HasConversion<string>();

Building the model looks something like this:
public override void Build(ModelBuilder modelBuilder)
{
    EntityTypeBuilder<Person> entityTypeBuilder =
        modelBuilder.Entity<Person>();

    entityTypeBuilder
        .Property(l => l.GUID)
        .HasConversion<string>();
}

Data type of GUIDs before:

Then after added these lines:


How to shut down Spring Boot Kafka stream-processing app after current message

Question: In a Spring Boot Kafka stream-processing app using Spring Cloud Function, how can I shut down the application (to stop it receiving any further messages) but only after responding to the current message?
The scenario is this: we have an app that processes a message which contains a reference to some file. We handle this by interrogating the given file's content using some third party libraries and responding with some data we extract from the file. In some rare cases certain files can cause the external libraries to hang. So we call these libraries on a background thread and time out if it takes too long. We need to respond to Kafka for this message (with some JSON detailing the error) so that Kafka doesn't send it to any other instance of our app (as it'll probably cause that instance to hang as well). But we then want this instance of our Spring Boot app to shut down, as we can't cleanly recover from the hang in the 3rd party library (we could get resource or memory leaks otherwise). Another instance will then be automatically brought up by Kubernetes or Docker Swarm or whatever.
I'm not sure if it's relevant, but we're using Spring Cloud Function, and we're joining two streams: the "file" stream, where each message contains a reference to the file to process, and a GlobalKTable with some config info. The code looks like this (in Kotlin):
    // The service we use to run the process on a background thread
    private val executorService = Executors.newFixedThreadPool(1)

    @Bean
    fun process() = BiFunction<KStream<String, FileInfo>,
        GlobalKTable<String, Config>,
        KStream<String, FileInterrogationResult>> { fileStream, configTable ->

        requestStream.join(
            configTable,
            { _, file -> file.configId },
            { file, config ->
                try {
                    // Process the file using the 3rd party libraries.
                    val result = executorService.submit(theThirdPartyLibExtractionFunction)
                        .get(someTimeout, TimeUnit.MILLISECONDS)

                    // Success: return some FileInterrogationResult object wrapping the result from above.
                } catch (e: TimeoutException) {
                    // Return some FileInterrogationResult object with error details.
                    // TODO: Here we know that after this function completes the app should shut down. How do we do this?
                } catch (e: Throwable) {
                    // Return some FileInterrogationResult object with error details.
                }
            }
        )

Answer: You can use Actuator endpoints to stop/start/pause/resume as well as monitor individual bindings.
Once enabled you can simply use curl to manage the bindings.
For example
curl -d '{"state":"STOPPED"}' -H "Content-Type: application/json" -X POST http://<host>:<port>/actuator/bindings/myBindingName

The exact instruction on how to do it available here
Comment: Thanks Oleg. Unfortunately it's more that we need to do it programmatically immediately when we encounter the timeout (where the TODO in the code snippet above is). Otherwise either we don't respond to Kafka, in which case another consumer will pick up the message and hang as well. Or we do respond to Kafka but don't shut down, in which case this current instance of the app will receive another message when it hasn't cleaned up from the previous request.
Comment: So just to check: are you suggesting that, where that TODO is in the code snippet above, the Spring Boot app make an HTTP POST request to itself?
Comment: I am not sure i understand your use case so I can't answer the "when/where" question. I just showed you a mechanism by which you can manage the bindings lifecycle. What would be better if you instead explain the business problem so we can find a right solution rather then ask how to enable specific solution which can very well be the wrong one.
Comment: Sorry, I thought the original post provided that info. We basically have the potential for a message which is a "poison pill" (though not because it can't be deserialised). When we process that message it hangs our consumer. So we want to respond to that message (so it doesn't get re-sent to another instance of our consumer). Then we want to shut down our consumer (the whole Spring Boot app in a Docker container) as it's now in an unstable state. Kubernetes will then fire up a new instance of the service, which will pick up the next message and resume as normal.
Comment: So, yes you can POST to stop the binding as described earlier. Given that we already expose this functionality via REST we can perhaps consider exposing it via API for cases like this, so feel free raise an issue and link to this post
Comment: Thanks! Logged here: https://github.com/spring-cloud/spring-cloud-stream/issues/2096


Extract words from sentence in python

Question: I have a dataset in a text/csv file format. It has 2 columns like this =
ID - TEXT
1 - this probability is 10-15%
2 - approximately 20% probablity
3 - 15% probability

I am trying to use NLTK to extract the number from the data where there is the keyword 'Probability' present.
This is what my code looks like.
import pandas as pd
import nltk
from nltk import sent_tokenize, word_tokenize

data_file = pd.read_excel(r'data_excel.xlsx',sheet_name = 'data')

df = pd.DataFrame(data_file, columns = ['ID','TEXT'])
keywords = ["probability"]

id_text = nltk.Text(str(df.ID).splitlines())
text_value = nltk.Text(str(df.TEXT).splitlines())

I want the output to look like this -
ID - Value
1 - 10
2 - 20
3 - 15

If someone can nudge in the right direction, it will be very helpful.
Answer: THIS CODE SHOULD WORK OR AT LEAST POINT YOU INTO SOLVING IT
Here is the full code
import csv
import nltk
impor re
import pandas as pd
from nltk import sent_tokenize, word_tokenize

tweet = []

data_file = pd.read_excel(r'data_excel.xlsx',sheet_name = 'data')
df = pd.DataFrame(data_file, columns = ['ID','TEXT'])

cols = ['ID', 'Num']
newDataFrame = pd.DataFrame(columns=cols)

#this should provide you with a list of both ID and txt
ID = df.iloc[:,0].values
TEXT  = df.iloc[:,1].values

#loop throug the id and set occurence of the number of probability
for i in range(1, len(ID)):
    number_list = re.findall(r'\b\d+\b', TEXT[i])

    newDataFrame.iloc[i].ID = ID
    newDataFrame.iloc[i].Num = number_list

print(newDataFrame)

Comment: thanks man..@sodimu-segun-michael  that helped... i am getting a error though.. for newDataFrame.iloc[i].ID = ID   , its saying single positional indexer is out of bounds
Comment: The code are untested codes and I am not on PC to test it.. Just find a simple way to add  records to dataframe via loop.. That's what the error is about..maybe I missed something small there. Don't forget to mark it as answer if it helped

Extract words from sentence in python


Wiring 2-way connected switch in 2-gang configuration

Question: I've done this a few times now, but I've come across an odd wiring setup that I need a bit of guidance with. The current setup isn't using a netural, which I need for the connected switch, but instead has a single black wire that's stripped in one place to connect the two switches. (Bottom pole, leftmost switch.)

My question is: based on this setup, what do I do with the single black wire, and what do I do with the maretted neturals? I'm guessing I have to split that wire into two segments, but I've never seen this sort of arrangement, so I'm a bit confused. Any info is very much appreciated.
Comment: Can you clarify what you're trying to install here? It's not clear if you're trying to put in a smart switch, or some other sort of thing altogether...
Comment: Yes, it's a smart switch. Leviton Z-wave.
Comment: Model # please?
Comment: It's a Leviton DZS15-1BZ Decora Z-Wave Controls 15-Amp Scene Capable Switch. I know how to install the switch normally though, my issue is more with the fact that there's no neutral to the existing switches, and there's no break in the jumper wire between the two switches, so I'm not sure what to do.
Answer: The black coming in to the double loaded screw is the always hot. It is then jumpered over to the other switch. Each then has a switched how leaving to the lights.
you can make this proper by using another short black wire and wirenutting it along with the existing jumper wire and the always hot. Then every screw in the switch will have exactly 1 wire connecting it as is proper.
As for the neutral you can add a white wire to the wirenut to jump it over to the smart switch (though you may need to get a larger size nut).
Comment: @RyanAnderson you have to add a wire to the neutral's wirenut to connect to the switch.
Comment: So, I'd cut the black jumper wire so it was two segments instead of one continuous one with a bare section connected to the screw? In terms of the neutral, there are three in the wirenut at the back. Do I just take one from that to connect it to the switch, and if so, which one?


NullReferenceException, ListView Object reference not set to an instance of an object

Question: I'm getting a "Object reference not set to an instance of an object" error with my code below and im not sure why. All my variables are being set, i even had some code before it checking if any of them are null and it never stopped there it just crashed on the line specified.
Code:
    private async void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
    {
        passArguments result = (passArguments)e.Argument;
        e.Result = result;

        while (running >= 5)
        {
            string[] row = { "Waiting", result.passedFileName };
            var listViewItem = new ListViewItem(row);
            listViewItem.ImageIndex = 1;
            listView1.Items[result.passedNum] = (listViewItem); //<--- error here

            await Task.Delay(500);
        }
        running++;
//other code
    }

    private async void backgroundWorker3_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //other code
        running--;
    }

Error:
An exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

Stack Trace
System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.ListView.ListViewNativeItemCollection.RemoveAt(Int32 index)
       at System.Windows.Forms.ListView.ListViewNativeItemCollection.set_Item(Int32 displayIndex, ListViewItem value)
       at System.Windows.Forms.ListView.ListViewItemCollection.set_Item(Int32 index, ListViewItem value)
       at ***.Form1.<backgroundWorker3_DoWork>d__c.MoveNext() in ***\Form1.cs:line 378
  InnerException:

Does anyone know what i'm doing here to cause this issue?
Comment: Code is a fair abomination, you'll have to make up your mind if you are going to use a BackgroundWorker or a Task or async/await, don't do *everything*.   What might happen is hard to guess without any context but the stack trace shows that the code does not run on the UI thread.  It is illegal to access a control from a worker thread.
Comment: Well, where does the debugger show the error?
Comment: @OldProgrammer On the line I said in the code above.
"istView1.Items[result.passedNum] = (listViewItem);"
Comment: 1 of 2 things is null.  `listView1`, or `result`  you can use the [Debugger](http://www.dotnetperls.com/debugging) to see which of them is null.  Visual Studio should set up a breakpoint for you when you hit an uncaught exception.
Comment: @SamIam, `Items[result.passedNum]` could also be null.
Comment: @SamIam It not hitting any of them
`if (listView1 == null)
                {
                    MessageBox.Show("lv1");
                }
                if (result == null)
                {
                    MessageBox.Show("resualt");
                }`
Comment: @AlexandreSeverino You are right it is null but why? `if (listView1.Items[result.passedNum] == null)
                {
                    MessageBox.Show("listView1.Items");
                }`
Comment: @RuneFS well observed. I wonder if OP can tell us what is the **type** of `listView1`.
Comment: @AlexandreSeverino if `Items[result.passedNum]` were null it would still run.
Comment: @SamIam, RuneFS just made me realize that. Thanks.
Comment: @AlexandreSeverino what do you mean? Its a System.Windows.Forms.ListView
Answer: Add the listViewItem if you can't find it on the Items collection.
if (!listView1.Items.Contains(result.passedNum))
    listView1.Items.Add(listViewItem);

Warning: this will work but you should consider a way to avoid too much searches on the list (Contains()) on larger iterations.
Comment: I tried
`if (listView1.Items.Count > result.passedNum)
                {
                    listView1.Items[result.passedNum] = (listViewItem);
                }`
but im still getting a "'System.NullReferenceException' " inside of the if statment
Comment: also adding a new one `listView1.Items.Add(listViewItem);` just spams the listview with new items, I shouldent need to use this as it should already exsist
Comment: What is the type of `listView1`?
Comment: Its a System.Windows.Forms.ListView
Comment: Do you know a better way to do this? what about a try statement, and if it fails it will then add the item?
Comment: Just do it outside of the loop. And I don't think a try/catch would be recommended. Avoid that as much as possible.
Comment: What if i just check if its null inside?
`if (listView1.Items[number] == null)
                    listView1.Items.Add(listViewItem);`


Spring boot application error constructor Order in class Order cannot be applied

Question: I am developing a spring boot sample application. The code just builds up an order list. But, I am getting the following error when i build on IntelliJ. The error looks, there is no constructor on Order (java) class, but I didn't find how to fix this kind of error.
OrderDao.java:
package com.example.demo.repository;

import com.example.demo.domain.Order;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Repository
public class OrderDao {
    public List<Order> buildOrders(){
        return Stream.of(
                new Order(101, "Mobile", 20.0, 1),
            new Order(102, "Book", 9.1, 2),
            new Order(278, "Book", 1.2, 3),
            new Order(953, "Jeans", 12.3, 1)
        ).collect(Collectors.toList());
    }
}

Order.java:
package com.example.demo.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor

public class Order {

    private int id;
    private String name;
    private double price;
    private int quantity;

}

Error when building in IntelliJ:
error: constructor Order in class Order cannot be applied to given types;
            new Order(101, "Mobile", 20.0, 1),
            ^

required: no arguments
found:    int,String,double,int
reason: actual and formal argument lists differ in length
Comment: `int,String,int,int` where's the `double`?
Answer: I guess your problem comes from lombok configurations:
For correctly configuring lombok on your spring boot app, you need few things

Need to enable annotations processor inside intellij. Go to settings->build,
execution, deployment-> Compiler -> Annotation processor: Verify that Enable
annotation processor checkbox is turn on.
Just add lombok dependency in pom.xml, with provided scope and without
version because in spring boot parent pom it is exists with correct version.

If this two step will not work, you can try this:
maven compiler plugin-> annotation processor path
Comment: Hi Razmik, I did the first point. Secondly, i am using gradle, not pom.xml. My build.gradle has implementation 'org.projectlombok:lombok:1.18.26'
Comment: For gradle you can try this one:

dependencies {
 compileOnly 'org.projectlombok:lombok:1.18.28'
 annotationProcessor 'org.projectlombok:lombok:1.18.28'
}
Comment: dependencies {
 implementation 'org.springframework.boot:spring-boot-starter-web'
 implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.projectlombok:lombok:1.18.28'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
 implementation 'com.amazonaws:aws-lambda-java-core:1.2.2'
 implementation 'com.amazonaws:aws-lambda-java-events:3.11.1'
 implementation 'com.amazonaws.serverless:aws-serverless-java-container-spring:1.5.2'
}
Comment: I have the above in build.gradle. but still this error is coming up
Comment: Can to check Order.class under target folder ?
Their take a look that lombok generates the constructors
Comment: Ok, just by control + click lombok.AllArgsConstructor; right? yes, it's opening
Comment: If their not exists constructor, you should add the compileOnly and annotationProcessor in gradle file, and after that you should build the project with gradle:
https://projectlombok.org/setup/gradle
Comment: No, with that approach you can see the AllArgsConstructor annotation implementation under the lombok. When you are running project javac compiles your .java files to .class files. In your example for Order.java file should be generated Order.class file. Lombok is injecting the Getter Setter AllArgsConstructor etc. in your Order.class (if lombok is working).


Unable to stream RTSP video in Android MediaPlayer

Question: I'm trying to stream a video from an RTSP server. I want to run the video inside my app. I have tried three different approaches and only one of them seems to work:

Using VideoView (does not work).
Using native player (works).
Using MediaPlayer (does not work).

I want to display the video on MediaPlayer but unable to do it.
Here is the code:
<code>@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /* 1. Using VideoView, error: "Cannot play video"
    VideoView vidView = (VideoView) findViewById(R.id.myVideo);

    String vidAdress = "rtsp://10.42.0.91:5554/camera";
    Uri vidUri = Uri.parse(vidAdress);

    vidView.setVideoURI(vidUri);

    vidView.start();

    */

    //Using native player (implicit intent), works.
    String mediaURL = "rtsp://10.42.0.91:5554/camera";
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mediaURL));
    startActivity(intent);

    /* Using MediaPlayer (doesn't work). Error: "Cannot create MediaPlayer"
and setDataSource.
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    try
    {
        mediaPlayer.setDataSource("rtsp://10.42.0.91:5554/camera");
        mediaPlayer.prepare();
        mediaPlayer.start();
    }
    catch (IOException ex)
    {
        ex.printStackTrace();
    }
    */
</code>
http:10.42.0.91:8080/playlist.m3u looks like this:
#EXTM3U
#PLAYLIST:RTSP Camera Server
 #EXTINF:-1, Active camera
rtsp://10.42.0.91:5554/camera
 #EXTINF:319, 3/3/16 PM 3:08:  35 MB
 rtsp://10.42.0.91:5554/record160303_1500
 #EXTINF:1047, 3/3/16 PM 2:15:  106.4 MB
 rtsp://10.42.0.91:5554/record160303_1400
 #EXTINF:1364, All records  141.4 MB
 rtsp://10.42.0.91:5554/record
 #EXTINF:-1, Back camera
 rtsp://10.42.0.91:5554/back
 #EXTINF:-1, Front camera
 rtsp://10.42.0.91:5554/front
Answer: I've just finished a basic application that can display RTSP H.264(Even high profile) in the Android MediaPlayer.
I was experimenting and researching how I should implement it, when I came across this video. It is quite easy to implement and source code is also provided.
I use Android version 5.1.1, it doesn't seem to work on 4.4 and below. My goal is to make it work on 4.1 and up, so I'll edit or comment when I got that working.
I hope this explains everything you need. Just tell me if it still does not work and I'll try to help you out.


Is a corner roller or corner pad a good tool to use to cut into corners when painting?

Question: We are preparing to paint some walls, and want to know if a corner roller or a corner pad are good tools to use for cutting in the corners.  We are hesitant to cut in with a brush since it leaves a different 'texture' than the 9" roller we will use for the rest of the wall.
Answer: I've tried a bunch of tools for cutting in corners/edges (the worst part of the job, IMO), and always end up going back to using a paintbrush.  There's not enough of a texture difference to notice it unless you have your face ON the wall (once it's dry), and it's much easier to deal with.
My family helps each other with painting, and my sister has textured walls. In that case we did use foam rollers to cut in, just for the better coverage in the texture. As long as you have plain, flat, drywall, brushes are the way to go IMO.
Comment: I agree with the brush idea for cutting in. Then it is not hard at all to bring the 9" roller right up near the corners and edges so only a very small margin of the brushed corner is there. After drying it is good as gone.


Including Null Results with get_results

Question: New to this, but I have the below problem:
Right now I have a working script, but if there is no $merchant result, I still want to display the form that sits below:
I've tried including "IS NULL" for this, but looking at similar examples I'm not sure if this is the right move?
Here is my working current code, any recommendation welcome:
<?php

if (is_user_logged_in()):

$merchant = wp_get_current_user()->display_name;

global $wpdb;
$customers = $wpdb->get_results("SELECT * FROM profile WHERE merchant = '$merchant';");

echo "<form>";

foreach($customers as $customer){
echo "
".$customer->merchant."
";
echo "Display name:
";
echo "".$customer->displayname."

";
echo "About you:
";
echo "".$customer->about."

";
echo "Address:
";
echo "".$customer->street."
";
echo "".$customer->area."
";
echo "".$customer->postal_code."
";
echo "".$customer->city."

";
echo "Website:
";
echo "".$customer->website."

";
};

echo "</form>";

else:
echo "Sorry, only registered users can view this information";
endif;

?>

Comment: Sure you really want to use the same form implementation? That would mean you need to provide a "dummy object" in your `$customers` array. So prior to the loop you check if that array is null or empty, if so you assign a one element array holding that dummy object.
Comment: @arkascha I could do that? However I do separate out $merchants so that I can use it through the rest of my code?
Comment: Sorry, I fail to understand what you are trying to say in your comment. Certainly you can create a dummy object, why not? But what are you trying to say about `$merchants` (I suppose you actually mean `$customers`?)?
Answer: Figured this out myself eventually - by adding a condition on whether the array was empty (using "if (empty($array))" , I can still generate the desired output as follows:
<?php

if (is_user_logged_in()):

$merchant = wp_get_current_user()->display_name;

global $wpdb;
$customers = $wpdb->get_results("SELECT * FROM profile WHERE merchant = '$merchant';");

echo "<form>";

foreach($customers as $customer){
echo "
".$customer->merchant."
";
echo "Display name:
";
echo "".$customer->displayname."

";
echo "About you:
";
echo "".$customer->about."

";
echo "Address:
";
echo "".$customer->street."
";
echo "".$customer->area."
";
echo "".$customer->postal_code."
";
echo "".$customer->city."

";
echo "Website:
";
echo "".$customer->website."

";
};

if (empty($customers)) {

echo "
".$merchant."
";
echo "Display name:
";
echo "Please add

";
echo "About you:
";
echo "Please add

";
echo "Address:
";
echo "Please add
";
echo "
";
echo "Website:
";
echo "Not here yet

";

}

echo "</form>";

else:
echo "Sorry, only registered users can view this information";
endif;

?>

Including Null Results with get_results


Form in menu ux design query

Question: So I have a use case where we have a button that is to trigger the "reassign" flow
this obviously needs a list of people to reassign to (1 or many)
I have a multiselect typeahead creating the list. This is consistent with the rest of the UI/UX design.
I am however not happy with the button creating a dropdown menu, and have the select +2 buttons (a short cut for "assign to me" and "assign")
I do not want a modal. Any other thoughts of how to make this better both UX and UI
Previous:

ATTEMPT 1:
Comment: Do you need the "assign" button or could it be part of the select interface? Something like a checkmark that appears only when a username is matched and sits inside the element would work. Also, I assume users are going to be interacting with the keyboard to search users so binding the enter key to "complete" the action would be expected. I think that the more this LOOKS like a traditional input form, the more you'll struggle with it.
Comment: it's a multi-select so can have lots of users, and we want to avoid setting assignees everytime one is selected (as the logs would fill up if nothing else) hence the assign button...
Comment: In that case, are there checkboxes (or some other selection technique) in the type-ahead list? I'm mostly responding to the size and placement of that button. I think it could appear inline with the list and be quite a bit smaller. How familiar are users with this list and how frequently is "assign to me" the needed option? If those are both high, you could remove the "assign to me" button and default the list item to "me" with my name selected. This removes the multiple interaction paradigms.
Answer: I don't think the elements or interactions are really the problems here. In my opinion, the visual design is causing the catch. Elements don't have any spacing around them so it means they feel detached from the "menu" trigger.
I would suggest making the background obviously unique and when "open" it should wrap around the trigger element AND expand with the content. Start with just the assign to me button and the search field then expand the background WITH the search field when it expands. I would guess the part that's bugging you is that it feels like two select lists on top of each other. Building a container that responds to the other elements would fix that.
Comment: thank's for this. I have yet to put in the "assign to me" button but have updated original posst to include what I now have. thoughts/opinions very welcome

Form in menu ux design query