Q: When parsing a string in to DateTime, parse the string to take data before putting each variable into the DateTime Object I have checked all answer related to this error but I couldn't solve it.
I have an admission form. They are fields like DateOfAdmission, PhoneNumber **, **TimeOfAdmission and other things . When I try to retrieve values from the form and convert them. And then try to insert them in SQL Server Database. The application breaks saying...

'System.FormatException'
  When parsing a string into DateTime, parse the string to take data before putting each variable into the DateTime Object.

Here is my code please look at it and tell me where I am getting wrong.
public partial class AdmissionForm : Window
{
    public AdmissionForm()
    {
        InitializeComponent();
    }

    private void SubmitFormClick(object sender, RoutedEventArgs e)
    {
        // Parsing Values

        // STIRNG TO INT
        int AdmissionNumber = int.Parse(AdmissionNumberTextBox.Text);
        int CNIC = int.Parse(CNICTextBox.Text);
        int Age = int.Parse(AgeTextBox.Text);
        int PhoneNumber = int.Parse(PhoneTextBox.Text);
        int MobileNumber = int.Parse(MobileTextBox.Text);
        int EmergencyNumber = int.Parse(EmergencyTextBox.Text);

        // Converting time 'String' into Byte[]
        //string time = DateTime.Now.ToString("hh:mm:tt"); //TimeOfDay.ToString("HH:mm:ss")
        byte[] TimeOfAdmission = Encoding.ASCII.GetBytes(DateTime.Now.ToString("hh:mm:tt"));

        // Converting date 'String' into DateTime
        //string date = DateTime.Now.ToString("dd/MM/yyyy");
        DateTime DateOfAdmission = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));

        // String
        var StudentName = StudentNameTextBox.Text;
        var FatherName = FatherNameTextBox.Text;
        var Qualification = QualificationTextBox.Text;
        var Nationality = NationalityTextBox.Text;
        var Adress = AdressTextBox.Text;
        var Timing = SheduleTime.SelectedValue.ToString();
        var ClassLevel = ClassValue.SelectedValue.ToString();
        var Reference = ReferenceTextBox.Text;

        // DB context Getting Entites and attributes
        var context = new AcademyEntities();
        // Creating new Model
        var Model = new StudentTable();
        // Getting and Setting values
        // Reading Values

        Model.StudentName = StudentName;
        Model.FatherName = FatherName;
        Model.StudentCNIC = CNIC;
        Model.GuardianName = null;
        Model.Nationality = Nationality;
        Model.Qualification = Qualification;
        Model.StudentAge = Age;
        Model.AdmissionNumber = AdmissionNumber;
        Model.TimeOfAdmission = TimeOfAdmission;
        Model.DateOfAdmission = DateOfAdmission;
        Model.MobileNumber = MobileNumber;
        Model.PhoneNumber = PhoneNumber;
        Model.EmergencyNumber = EmergencyNumber;
        Model.Adress = Adress;
        Model.ClassID = ClassTime(Timing);
        Model.CourseLevelID = CourseLevelID(ClassLevel);
        Model.Reference = Reference;

        context.StudentTables.Add(Model);
        context.SaveChanges();

    // Class Time Id
    int ClassTime(string Timing)
    {
        int classId = 1;
        if (Timing == "2 to 3 PM")
        {
            return classId;

        }
        else if (Timing == "3 to 4 PM")
        {
            classId = 2;
            return classId;
        }
        else if (Timing == "4 to 5 PM")
        {
            classId = 3;
            return classId;
        }
        else if (Timing == "5 to 6 PM")
        {
            classId = 4;
            return classId;
        }
        else if (Timing == "6 to 7 PM")
        {
            classId = 5;
            return classId;
        }
        return classId;
    }
    // Course Level Id
    int CourseLevelID(string ClassLevel)
    {
        int courseLevelId = 1;
        if (ClassLevel == "Lower Basic")
        {
            return courseLevelId;

        }
        else if (ClassLevel == "Basic")
        {
            courseLevelId = 2;
            return courseLevelId;
        }
        else if (ClassLevel == "Pre Starter")
        {
            courseLevelId = 3;
            return courseLevelId;
        }
        else if (ClassLevel == "Starter")
        {
            courseLevelId = 4;
            return courseLevelId;
        }
        else if (ClassLevel == "Intermediate")
        {
            courseLevelId = 5;
            return courseLevelId;
        }

        else if (ClassLevel == "Advance")
        {
            courseLevelId = 5;
            return courseLevelId;
        }
        return courseLevelId;
    }
}

A: Well I searched and found one solution to parse date as SQL SERVER timestamp
string currentTime = DateTime.Now.ToString("hh:mm:tt");
byte[] TimeOfAdmission = Encoding.ASCII.GetBytes(currentTime);


Q: Multiple git subtree of same repo In a repo called apps I'm stuck with using a folder structure like
/apps
 - /app1
   - /shared
 - /app2
   - /shared
 - /app3
   - /shared

where shared is a separate repo. How do I use git subtree such that changes to any app1/shared, app2/shared, app3/shared take effect in each other and can be pushed back to the shared repo?


Q: STM32CubeMx: How to add your own "USER CODE BEGIN / END" section? I have a project where I need to add a few lines to one of the generated C files. However, the place where I want to add the change does not have a "USER CODE BEGIN / END" section. So whenever, I regenerate code, the changes are overwritten. I tried adding my own user code section as shown  below but even that got overwritten. It seems, CubeMX is looks for a predefined set of USER CODE blocks, and overwrites everything else.
/* USER CODE BEGIN 8 */

/* USER CODE END 8 */

I would like to be able to define my own user code blocks so that I can write custom code in places where CubeMX has not already provided a user code block.

A: Adding custom user code sections is not supported by CubeMX.
See this support post:
https://community.st.com/s/question/0D50X0000ALxNlmSQF/is-it-possible-to-add-custom-user-code-sections


Q: Empty file when using sprintf and system function on C I want to save some information in a file text, I wrote this program:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

int main(int argc,char *argv[])
 {
     FILE *fichier;
     char buffer[20];
     char command[200];
     char command1[100];

     system(" cat /etc/resolv.conf  | grep nameserver | awk -F' ' '{print $2}' | cut -d'.' -f1-3 | awk '{print $1\".1\"}' > ethernet_dns.txt");

     fichier=fopen("ethernet_dns.txt","r");
     memset(&buffer,0,sizeof(buffer));
     fread(buffer,20,1,fichier);
     printf("buffer is: %s",buffer);

     snprintf(command,sizeof(command),"ping -c 1 -W 1  %s > /tmp/ping_result",buffer);
      printf("command is: %s",command);

     system(command);

     return 0;
 }

Results:
buffer is: 127.0.1.1
command is : ping -c 1 -W 1 127.0.1.1

the system command returns this:
    PING 127.0.1.1 (127.0.1.1) 56(84) bytes of data.
64 bytes from 127.0.1.1: icmp_seq=1 ttl=64 time=0.115 ms

--- 127.0.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.115/0.115/0.115/0.000 ms

But when I run : cat /tmp/ping_result.I got an empty file

A: Problem: Code is reading '\n' into buffer[] yet trying to that as part of the command.  Need to trim the buffer.  *** below
 // Insure file is open
 fichier=fopen("ethernet_dns.txt","r");
 assert(fichier);

 // Use fgets
 //memset(&buffer,0,sizeof(buffer));
 //fread(buffer,20,1,fichier);
 if (fgets(buffer, sizeof buffer, fichier)) {

   // lop off potential \n
   buffer[strcspn(buffer, "\n")] = '\0';  // ***

   printf("buffer is: <%s>\n",buffer);

   int n = snprintf(command, sizeof(command), "ping -c 1 -W 1  %s > /tmp/ping_result",
     buffer);
   printf("command is: <%s>\n",command);

   // Only issue command if no problems occurred in snprintf()
   if (n > 0 && n < sizeof(command)) system(command);

A: the posted code has a couple of problems
1) outputs results of ping to stdout rather than to /tmp/ping_result
2) fails to removed trailing newline from the buffer[] array
The following code
1) cleans up the indenting
2) corrects the problems in the code
3) handles possible failure of call to fopen()
4) eliminates unneeded final statement: return 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( void )
{
     FILE *fichier;
     char buffer[20];
     char command[200];

     system(" cat /etc/resolv.conf  | grep nameserver | awk -F' ' '{print $2}' | cut -d'.' -f1-3 | awk '{print $1\".1\"}' > ethernet_dns.txt");

     fichier=fopen("ethernet_dns.txt","r");
     if( !fichier )
     {
         perror( "fopen for ethernet_dns.txt failed");
         exit( EXIT_FAILURE );
     }

     // implied else, fopen successful

     memset(buffer,0,sizeof(buffer));
     size_t len = fread(buffer,1, sizeof(buffer),fichier);
     printf( "len is: %lu\n", len );
     buffer[len-1] = '\0'; // eliminates trailing newline
     printf("buffer is: %s\n",buffer);

     snprintf(command,sizeof(command),"ping -c 1 -W 1 ");
     strcat( command, buffer);
     strcat( command, " > /tmp/ping_result");
     printf("command is: %s\n",command);

     system(command);
}

the resulting output, on my computer, is in file: /tmp/ping_result
PING 127.0.1.1 (127.0.1.1) 56(84) bytes of data.
64 bytes from 127.0.1.1: icmp_seq=1 ttl=64 time=0.046 ms

--- 127.0.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.046/0.046/0.046/0.000 ms


Q: ASP.NET Core ILogger does not log to Trace unless a debugger is attached I am having a weird situation where I cannot receive any Trace from ASP.NET Core ILogger<T> unless a debugger is attached (even if I am running in Debug configuration). First let me explain:
I need to write logs to files by date, and I previously wrote a simple custom TraceListener for that, so I think I can reuse it without writing new Log Provider:
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging(logging =>
            {
                logging.AddTraceSource("IISLogCleaner");
                // I added below line to make sure I am not missing anything
                logging.SetMinimumLevel(LogLevel.Trace);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

In Startup.ConfigureServices(), I add the listener:
            var listener = new DailyTraceListener(folder);
            Trace.Listeners.Add(listener);

Now for the logging background task:
    ILogger<CleanupTask> logger;

    public CleanupTask(ILogger<CleanupTask> logger)
    {
        this.logger = logger;
    }

    // ...

    async Task<int> WorkAsync(CancellationToken token)
    {
        System.Diagnostics.Debug.WriteLine("Start Working");
        this.logger.LogInformation("Start working");

        await Task.Delay(1000);

        this.logger.LogInformation("Work finished");
        return await Task.FromResult(10000);
    }

Here's the problem: with debugger attached, all the logs are written to log file. However, when I choose to Run without Debugging, even in Debug configuration, no file is created, no content is written, until I added the Debug.WriteLine calls, now only these lines get logged, but all the logger.LogInformation() are not recorded.

The Output window however, receive all the messages:

To be safe, I have deleted the appsettings.Development.json file and only appsettings.json remains. Here is the Log content:
"Logging": {
    "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
    }
},

A: Ages later, I know but I believe you are hitting this:
ASP.Net Core Logging and DebugView.exe
I spent 3 hours on this one today and feel your paint

TL;DR: Debugger.IsAttached is checked within Microsoft.Extensions.Logging.Debug.


Q: Efficient way of calculating minimum distance between point and multiple faces I have multiple faces in 3D space creating cells. All these faces lie within a predefined cube (e.g. of size 100x100x100).
Every face is convex and defined by a set of corner points and a normal vector. Every cell is convex. The cells are result of 3d voronoi tessellation, and I know the initial seed points of the cells.
Now for every integer coordinate I want the smallest distance to any face.
My current solution uses this answer https://math.stackexchange.com/questions/544946/determine-if-projection-of-3d-point-onto-plane-is-within-a-triangle/544947 and calculates for every point for every face for every possible triple of this faces points the projection of the point to the triangle created by the triple, checks if the projection is inside the triangle. If this is the case I return the distance between projection and original point. If not I calculate the distance from the point to every possible line segment defined by two points of a face. Then I choose the smallest distance. I repeat this for every point.
This is quite slow and clumsy. I would much rather calculate all points that lie on (or almost lie on) a face and then with these calculate the smallest distance to all neighbour points and repeat this.
I have found this Get all points within a Triangle but am not sure how to apply it to 3D space.
Are there any techniques or algorithms to do this efficiently?

A: Since we're working with a Voronoi tessellation, we can simplify the current algorithm. Given a grid point p, it belongs to the cell of some site q. Take the minimum over each neighboring site r of the distance from p to the plane that is the perpendicular bisector of qr. We don't need to worry whether the closest point s on the plane belongs to the face between q and r; if not, the segment ps intersects some other face of the cell, which is necessarily closer.
Actually it doesn't even matter if we loop r over some sites that are not neighbors. So if you don't have access to a point location subroutine, or it's slow, we can use a fast nearest neighbors algorithm. Given the grid point p, we know that q is the closest site. Find the second closest site r and compute the distance d(p, bisector(qr)) as above. Now we can prune the sites that are too far away from q (for every other site s, we have d(p, bisector(qs)) ≥ d(q, s)/2 − d(p, q), so we can prune s unless d(q, s) ≤ 2 (d(p, bisector(qr)) + d(p, q))) and keep going until we have either considered or pruned every other site. To do pruning in the best possible way requires access to the guts of the nearest neighbor algorithm; I know that it slots right into the best-first depth-first search of a kd-tree or a cover tree.


Q: Is there a way to take a ECS TaskDefinition as an input to a CloudFormation template? I am new to AWS CloudFormation. I have been testing out automating deployments of containers using this service. However, as of now, I am manually adding the name of my TaskDefinition to the YAML file of the CloudFormation template. Is there a way for me to define this as an input.

A: Yes, you can use stack output exports, define export value on one stack and import it in any other: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-exports.html


Q: What is the simplest way to sort the array of object? I want to sort an array like this
[
    {
        'type': 'apple',
        'like': 3
    },
    {
        'type': 'pear',
        'like': 5
    },
    ...
]

I was using lodash lib for the sorting.
Is there a any more simple way to sort an array by like value?

A: How about pure JS sort method:

var k=[{'type': 'apple', 'like': 7}, {'type': 'pear', 'like': 5},{'type': 'pear', 'like': 10}];

console.log(k.sort((a,b)=>a.like-b.like));

If you want to understand it better, read it here.

I hope this helps. Thanks!

A: You can directly do this in javascript in the following manner:

var my_arr = [{'type': 'apple', 'like': 3}, {'type': 'pear', 'like': 5}, {'type': 'pea', 'like': 7}, {'type': 'orange', 'like': 1}, {'type': 'grape', 'like': 4}]

console.log(my_arr)

my_arr.sort((a, b) => (a.like > b.like) ? 1 : -1)

console.log(my_arr)

where my_arr is the original array you were attempting to sort


Q: Giving each subset of records a group ID I need to assign a rank to a subset of records.
Sample data:
Colour Code        Size Code
DWMUL040             7
DWMUL040             8
DWMUL040             9
DWMUL040             10
DWMUL040             7
DWMUL040             8
DWMUL040             9
DWMUL040             10
DWMUL040             7
DWMUL040             8
DWMUL040             9
DWMUL040             10
DWMUL040             7
DWMUL040             8
DWMUL040             9
DWMUL040             10

I need the data to look like this:
Group ID            Colour Code        Size Code
1                   DWMUL040             7
1                   DWMUL040             8
1                   DWMUL040             9
1                   DWMUL040             10
2                   DWMUL040             7
2                   DWMUL040             8
2                   DWMUL040             9
2                   DWMUL040             10
3                   DWMUL040             7
3                   DWMUL040             8
3                   DWMUL040             9
3                   DWMUL040             10
4                   DWMUL040             7
4                   DWMUL040             8
4                   DWMUL040             9
4                   DWMUL040             10

The list will be ordered, and basically, whenever the Size Code changes, it is a new group. I have a feeling this one is going to be hard, as the size code (these are garment sizes) could be anything "Such as XL".

A:   select [colour code], [size code],
         row_number() over (partition by [colour code], [size code]
                            order by 1/0) [group id]
    from tbl
order by [group id], [colour code], [size code];


Q: Correct concurrency handling using EF Core 2.1 with SQL Server I am currently working on an API using ASP.NET Core Web API along with Entity Framework Core 2.1 and a SQL Server database. The API is used to transfer money from two accounts A and B. Given the nature of the B account which is an account that accepts payments, a lot of concurrent requests might be executed at the same moment. As you know if it's not well managed, this can result in some users not seeing their payments arrive.
Having spent days trying to achieve concurrency I can't figure out what the best approach is. For the sake of simplicity I created a test project trying to reproduce this concurrency issue.
In the test project, I have two routes: request1 and request2 each one perform a transfer to the same user the first one have an amount of 10 and the second one is 20. I put a Thread.sleep(10000) on the first one as follows:
    [HttpGet]
    [Route("request1")]
    public async Task<string> request1()
    {
        using (var transaction = _context.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
        {
            try
            {
                Wallet w = _context.Wallets.Where(ww => ww.UserId == 1).FirstOrDefault();
                Thread.Sleep(10000);
                w.Amount = w.Amount + 10;
                w.Inserts++;
                _context.Wallets.Update(w);
                _context.SaveChanges();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
        }
        return "request 1 executed";
    }

    [HttpGet]
    [Route("request2")]
    public async Task<string> request2()
    {
        using (var transaction = _context.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
        {
            try
            {
                Wallet w = _context.Wallets.Where(ww => ww.UserId == 1).FirstOrDefault();
                w.Amount = w.Amount + 20;
                w.Inserts++;
                _context.Wallets.Update(w);
                _context.SaveChanges();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
        }
        return "request 2 executed";
    }

After executing request1 and request2 after in a browser, the first transaction is rolled back due to:
InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseSqlServer' call.

I can also retry the transaction but isn't there a better way? using locks ?
Serializable, being the most isolated level and the most costly too is as said in the documentation:

No other transactions can modify data that has been read by the current transaction until the current transaction completes.

Which means no other transaction can update data that has been read by another transaction, which is working as intended here since the update in the request2 route wait for the first transaction (request1) to commit.
The problem here is we need to block read by other transactions once the current transaction has read the wallet row, to solve the problem I need to use locking so that when the first select statement in request1 executes, all the transactions after need to wait for the 1st transaction to finish so they can select the correct value. Since EF Core have no support for locking I need to execute a SQL query directly, so when selecting the wallet I'll add a row lock to the current row selected
//this locks the wallet row with id 1
//and also the default transaction isolation level is enough
Wallet w = _context.Wallets.FromSql("select * from wallets with (XLOCK, ROWLOCK) where id = 1").FirstOrDefault();
Thread.Sleep(10000);
w.Amount = w.Amount + 10;
w.Inserts++;
_context.Wallets.Update(w);
_context.SaveChanges();
transaction.Commit();

Now this works perfectly even after executing multiple request the result of the transfers all combined is correct. In addition to that am using a transaction table that holds every money transfer made with the status to keep a record of each transaction in case something went wrong am able to compute all wallets amount using this table.
Now there are other ways of doing it like:

*

*Stored procedure: but I want my logic to be in the application level

*Making a synchronized method to handle the database logic: this way all the database requests are executed in a single thread, I read a blog post that advise about using this approach but maybe we'll use multiple servers for scalability

I don't know if I'm not searching well but I can't find good material for handling pessimistic concurrency with Entity Framework Core, even while browsing Github, most of code I've seen don't use locking.
Which bring me to my question: is this the correct way of doing it?
Cheers and thanks in advance.

A: My suggestion for you is to catch on DbUpdateConcurrencyException and use entry.GetDatabaseValues(); and entry.OriginalValues.SetValues(databaseValues); into your retry logic. No need to lock the DB.
Here is the sample on EF Core documentation page:
using (var context = new PersonContext())
{
    // Fetch a person from database and change phone number
    var person = context.People.Single(p => p.PersonId == 1);
    person.PhoneNumber = "555-555-5555";

    // Change the person's name in the database to simulate a concurrency conflict
    context.Database.ExecuteSqlCommand(
        "UPDATE dbo.People SET FirstName = 'Jane' WHERE PersonId = 1");

    var saved = false;
    while (!saved)
    {
        try
        {
            // Attempt to save changes to the database
            context.SaveChanges();
            saved = true;
        }
        catch (DbUpdateConcurrencyException ex)
        {
            foreach (var entry in ex.Entries)
            {
                if (entry.Entity is Person)
                {
                    var proposedValues = entry.CurrentValues;
                    var databaseValues = entry.GetDatabaseValues();

                    foreach (var property in proposedValues.Properties)
                    {
                        var proposedValue = proposedValues[property];
                        var databaseValue = databaseValues[property];

                        // TODO: decide which value should be written to database
                        // proposedValues[property] = <value to be saved>;
                    }

                    // Refresh original values to bypass next concurrency check
                    entry.OriginalValues.SetValues(databaseValues);
                }
                else
                {
                    throw new NotSupportedException(
                        "Don't know how to handle concurrency conflicts for "
                        + entry.Metadata.Name);
                }
            }
        }
    }
}

A: Why don't you handle the concurrency problem in the code, why it needs to be in the DB layer?
You can have a method that updates the value of given wallet with given value and you can use simple lock there. Like this:
private readonly object walletLock = new object();

public void UpdateWalletAmount(int userId, int amount)
{
    lock (balanceLock)
    {
         Wallet w = _context.Wallets.Where(ww => ww.UserId == userId).FirstOrDefault();
         w.Amount = w.Amount + amount;
         w.Inserts++;
         _context.Wallets.Update(w);
         _context.SaveChanges();
    }
}

So your methods will look like this:
[HttpGet]
[Route("request1")]
public async Task<string> request1()
{
    try
    {
        UpdateWalletAmount(1, 10);
    }
    catch (Exception ex)
    {
        // log error
    }
    return "request 1 executed";
}

[HttpGet]
[Route("request2")]
public async Task<string> request2()
{
    try
    {
        UpdateWalletAmount(1, 20);
    }
    catch (Exception ex)
    {
        // log error
    }
    return "request 2 executed";
}

You don't even need to use a transaction in this context.

A: You can use distributed lock mechanism with redis for example.
Also, you can lock by userId, it will not lock method for others.


Q: Get count of an item in a column when values in other column are equal to some value for i in range(0,9):
    num = training_data[training_data[i]==row[i]&training_data[9]==1].groupby([i,9]).size()
    print num
    count_equals.append(num)

DataFrame

      0  1  2   3  4   5  6  7  8  9

314   1  1  1   1  1   1  2  1  1  1
431   5  1  1   3  4   1  3  2  1  1
260  10  5  8  10  3  10  5  1  3 -1
91    3  1  1   2  2   1  1  1  1  1
337   1  1  1   1  2   1  3  1  1  1

I need counts into a list else groupby without second condition works.
if row =[1,1,1,1,1,1,1,1]
then the count_equals list should be [2,4,4,2,4,1,3,4]

Error:
  Traceback (most recent call last):
    File "naive.py", line 46, in
      num = training_data[training_data[i]==row[i]&training_data[9]==1].groupby([i,9]).size()
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 917, in nonzero
      .format(self.class.name))
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

A: In training_data[i]==row[i]&training_data[9]==1, the operator & has higher precedence than ==. Surround the relational expressions with parentheses before doing the AND:
(training_data[i]==row[i]) & (training_data[9]==1)


Q: Query with a data frame and a database I'd like to know if it's possible to make a query between a data frame and a database like this :
test <-  sqlQuery(ch,"
                  SELECT *
                  FROM table_from_database as A, dataframe as B
                  WHERE  a.id=b.id ")

A: Use the RODBC package to connect to a MS SQL Server database.
First you need to do some setup.  Open the "Data Sources (ODBC)" application.  (In Control Panel\System and Security\Administrative Tools, or search under the Start Menu.)  Add a User DSN (or a System DSN if you have admin rights and want the connection for all users).
Step 1: Give it a name like MyDataBase and select the server that it lives on.  The name shouldn't be more than 32 characters or you'll get a warning.
Step 2: Connection details are the same as you would use in SQL Server.
Step 3: Change the default database to the one that you want to connect to.
Finish and test your connection.
Now you get to use R.  It's as easy as
library(RODBC)
channel <- odbcConnect("MyDataBase") #or whatever name you gave
query <- "SELECT * FROM MyTable WHERE x > 10"
results <- sqlQuery(query, channel)
odbcClose(channel)

If you are feeling fancy or hate wizards, you can set up the ODBC connection by writing registry entries.  Apologies for the big code chunk.
#' Reads the Windows registry
#'
#' Wrapper for readRegistry that replace environment variables.
#' @param ... Passed to readRegistry
#' @return A list of registry keys.  See \code{readRegistry}.
#' @examples
#' \dontrun{
#' key <- "Software\\ODBC\\ODBCINST.INI\\SQL Server"
#' hive <- "HLM"
#' read_registry(key, hive)
#' readRegistry(key, hive)
#' }
read_registry <- function(...)
{
  ans <- readRegistry(...)
  lapply(
    ans,
    function(x)
    {
      rx <- "%([[:alnum:]]+)%"
      if(is.character(x) && grepl(rx, x))
      {
        env_var <- stringr::str_match(x, rx)[, 2]
        x <- gsub(rx, Sys.getenv(env_var), x)
      }
      x
    }
  )
}

#' Add an ODBC data source to the Windows registry.
#'
#' Adds an ODBC data source to the Windows registry.
#'
#' @param data_source_name String specifying the name of the data source to add.
#' @param database The name of the database to use as the default.
#' @param database_server The name of the server holding the database.
#' @param type Type of connection to add.  Either ``sql'' or ``sql_native''.
#' @param permission Whether the connection is for the user or the system.
#' @return Nothing.  Called for the side-effect of registering ODBC data sources.
#' @details A key with the specified data source name is created in
#' ``Software\\ODBC\\ODBC.INI'', either in ``HKEY_CURRENT_USER'' or
#' ``HKEY_LOCAL_MACHINE'', depending upon the value of \code{permission}.
#' Four values are added to this key.  ``Database'' is given the value of the
#' \code{database} arg.  ``Server'' is given the value of the
#' \code{database_server} arg. ``Trusted_Connection'' is given the value ``Yes''.
#' ``Driver'' is given the value from the appropriate subkey of
#' ``HKEY_LOCAL_MACHINE\\SOFTWARE\\ODBC\\ODBCINST.INI'', depending upon the type.
#' Another key with the specified data source name is created in
#' ``Software\\ODBC\\ODBC.INI\\ODBC Data Sources''.
register_odbc_data_source <- function(data_source_name, database, database_server, type = c("sql", "sql_native"), permission = c("user", "system"))
{
  #assert_os_is_windows()

  #data_source_name <- use_first(data_source_name)

  permission <- match.arg(permission)
  type <- match.arg(type)

  #Does key exist?
  odbc_key <- readRegistry(
    file.path("Software", "ODBC", "ODBC.INI", fsep = "\\"),
    switch(permission, user = "HCU", system = "HLM")
  )
  if(data_source_name %in% names(odbc_key))
  {
    message("The data source ", sQuote(data_source_name), " already exists.")
    return(invisible())
  }

  hive <- switch(
    permission,
    user   = "HKEY_CURRENT_USER",
    system = "HKEY_LOCAL_MACHINE"
  )
  key <- shQuote(
    file.path(hive, "Software", "ODBC", "ODBC.INI", data_source_name, fsep = "\\")
  )
  odbc_data_sources_key <- shQuote(
    file.path(hive, "Software", "ODBC", "ODBC.INI", "ODBC Data Sources", fsep = "\\")
  )

  type_name <- switch(
    type,
    sql = "SQL Server",
    sql_native = "SQL Server Native Client 11.0"
  )
  driver <- read_registry(
    file.path("SOFTWARE", "ODBC", "ODBCINST.INI", type_name, fsep = "\\"),
    "HLM"
  )$Driver

  system0(key)
  system0(key, "/v Database /t REG_SZ /d", database)
  system0(key, "/v Driver /t REG_SZ /d", shQuote(driver))
  system0(key, "/v Server /t REG_SZ /d", database_server)
  system0(key, "/v Trusted_Connection /t REG_SZ /d Yes")
  system0(odbc_data_sources_key, "/v", data_source_name, "/t REG_SZ /d", shQuote(type_name))
}

#' Wrapper to system for registry calls
#'
#' Wraps the \code{system} function that calls the OS shell.
#' @param ... Passed to \code{paste} to create the command.
#' @return The command that was passed to system is invisibly returned.
#' @note Not meant to be called directly.
system0 <- function(...)
{
  cmd <- paste("reg add", ...)
  res <- system(cmd, intern = TRUE)
  if(res != "The operation completed successfully.\r")
  {
    stop(res)
  } else
  {
    message(res)
  }
  invisible(cmd)
}


Q: Execution failed for task ':app:compileDebugJavaWithJavac'. Error:(2055, 52) error: ';' expected Error:(2055, 59) error:  expected I am new to android, my project was compiling and running properly a few moments ago but after I try to implement a navigation drawer, its giving me this error

FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED

Its also complaining about a line in this generated file

Error:(2055, 52) error: ';' expected
Error:(2055, 59) error:  expected
C:\Users\muoki\AndroidStudioProjects\MaterialTest\app\build\generated\source\r\debug\com\muoki\materialtest\R.java

which is here
public static final int fragment_navigation-drawer=0x7f0c0068;

I have tried running running using the script parameter as in explained in this question but its still giving the same error
Here is my gradle
 apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.muoki.materialtest"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:support-v4:22.2.1'
}

A: Well the first thing you should do is remove the code you added and get back to a version that complies. Then try again. You should also supply some code because the errors are not enough to answer this problem on their own.
You should also know that the R.java file is created each time you compile the app. The error from the R.java file probably indicates that there is a problem with the way you have coded the fragment navigation drawer. Check to see if you have used any spaces in the name, the R.java error shows an underscore and a hyphen. This may indicate that there is a problem with the name.
I would also recommend looking up some youtube videos explaining how to use the LogCat output to identify errors.
You should also read through the Google documentation on navigation drawer http://developer.android.com/training/implementing-navigation/nav-drawer.html and compare the example code with the code you have written.

A: Its smart answer is that there is some syntax error in your Code may be in Java file or XML.
Fix them first. Error will gone.  work 100 percent for me every time

  Here is my Error! I found it & remove then this compile time error resolve.


Q: Read and write NFC tag at once Is it possible to read/write NFC-tag simultaneously with Android API?
For example I have the counter in NDEF message and would like to read it, increment and write it back. Or read authentication key and update it.
I can see only one operation as per example.

A: Android’s Near Field Communication documentation page states that it is indeed possible.

Android-powered devices with NFC simultaneously support three main modes of operation:

*

*Reader/writer mode, allowing the NFC device to read and/or write passive NFC tags and stickers.
  . . .

A: Android will in the default setup automagically read any NDEF message and deliver it to your activity by invoking onNewIntent(..). Your app is then free to write to the tag - and can do so within the within the invokation to onNewIntent(..) or the following call to onResume(..).

A: i'm note sure if looking for react native for example, but you can do that from once inside a scope of a function that returns you the message and you can write immediately what you want.
The package is this one:
https://github.com/whitedogg13/react-native-nfc-manager
The feature is in the branch (tag-tech) open to discussion and sooner or later will be at master.


Q: Smaato custom banner view could not be instantiated (Android) I tried to use Smaato ad in my app. For first look it's simple to use, but in practice I got a problem. I put in main view XML a Smaato banner. The problem number one is that Eclipse shows me in GraphicLayout tab the next error message:
*

The following classes could not be instantiated:
  - com.smaato.SOMA.SOMABanner See the Error Log (Window > Show View) for more details. Tip: Use View.isInEditMode() in your custom views to
  skip code when shown in Eclipse

*
main.xml file code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RootLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <com.smaato.SOMA.SOMABanner
    android:id="@+id/bannerView"
    android:layout_width="fill_parent"
    android:layout_height="90dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
  />
</LinearLayout>

Accordingly to official SOMA SDK Developer guide:
1) I defined a banner view in my application (described above)
2) In code I add manully to onCreate method:
SOMABanner mBanner = (SOMABanner)findViewById(R.id.BannerView);
//In order to fetch live ads inside the activity, add your PublisherID and AdspaceID in the
//onCreate method. For example:
mBanner.setPublisherId(my_publisher_id);
mBanner.setAdSpaceId(my_adspace_id);

When I ran a programm, throws an exception:

12-20 03:00:48.415: ERROR/AndroidRuntime(12819): FATAL EXCEPTION: main
  12-20 03:00:48.415: ERROR/AndroidRuntime(12819):
  java.lang.RuntimeException: Unable to start activity
  ComponentInfo{com.example.android.My/com.example.android.My.App}:
  java.lang.ClassCastException: android.widget.TextView 12-20
  03:00:48.415: ERROR/AndroidRuntime(12819):     at
  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
  12-20 03:00:48.415: ERROR/AndroidRuntime(12819):     at
  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
  12-20 03:00:48.415: ERROR/AndroidRuntime(12819):     at
  android.app.ActivityThread.access$1500(ActivityThread.java:132) 12-20
  03:00:48.415: ERROR/AndroidRuntime(12819):     at
  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
  12-20 03:00:48.415: ERROR/AndroidRuntime(12819):     at
  android.os.Handler.dispatchMessage(Handler.java:99) 12-20
  03:00:48.415: ERROR/AndroidRuntime(12819):     at
  android.os.Looper.loop(Looper.java:143) 12-20 03:00:48.415:
  ERROR/AndroidRuntime(12819):     at
  android.app.ActivityThread.main(ActivityThread.java:4268) 12-20
  03:00:48.415: ERROR/AndroidRuntime(12819):     at
  java.lang.reflect.Method.invokeNative(Native Method) 12-20
  03:00:48.415: ERROR/AndroidRuntime(12819):     at
  java.lang.reflect.Method.invoke(Method.java:507) 12-20 03:00:48.415:
  ERROR/AndroidRuntime(12819):     at
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  12-20 03:00:48.415: ERROR/AndroidRuntime(12819):     at
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 12-20
  03:00:48.415: ERROR/AndroidRuntime(12819):     at
  dalvik.system.NativeStart.main(Native Method) 12-20 03:00:48.415:
  ERROR/AndroidRuntime(12819): Caused by: java.lang.ClassCastException:
  android.widget.TextView 12-20 03:00:48.415:
  ERROR/AndroidRuntime(12819):     at
  com.example.android.My.App.onCreate(Commander.java:132) 12-20
  03:00:48.415: ERROR/AndroidRuntime(12819):     at
  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
  12-20 03:00:48.415: ERROR/AndroidRuntime(12819):     at
  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
  12-20 03:00:48.415: ERROR/AndroidRuntime(12819):     ... 11 more

Who knows what is the problem? What I need to do to fix it? Any body works with Smaato?
Used Smaato SDK version 2.5.4

A: It shows you get a ClassCastException in your Commander class at line #132.
Please post the onCreate method of your Commander class or look into TextView casts in onCreate method.

A: Here is the solution what worked for me :
 - In eclipse, right click on the project-> properties -> Java Build Path -> Order and Export
Check the SOMA jar file in the path and try now !

A: I copied the Smaato jar into the libs/ directory; that seemed to help.


Q: Keycloack share same session between to client ID I'm not expert on this subject.
On Keycloak on the same realm we have defined two client ID.
My client ID are configured with OpenID connect and authorization code flow.
Like this a user can be authenticated with its credentials over these clients.
I have a problem. One of my user shares the same Keycloak session between these two differents client ID. In the fact, when user log on client A, he is logged out of client B. And same when he logs in client B, he is logged out of client A.
Why it's possible to share the same Keycloak session? And how to be sure to have two different Keycloak session.
UPDATE: I noticed, when the user logs in over client A or client B, he uses the same browser. If he logs in over client A, he doesn't need to enter its login/password over client B. The result is there is one Keycloak session. (If user uses different browser of each client, there is one keycloak session by client). Is it possible to force, one keycloak session by client ID?

A: What you are describing is a basic function in almost all AM software implementing OIDC, there is no such a thing as log in to clientA, the users always log in to the IDP, i.e. Keycloak.
Clients dont have sessions by default, keycloak does, and clients use keycloak session in thier OIDC flow.  For example if you are already authenticated to keycloak, and you tried to do OIDC flow with ClientA or ClientB, you wont be prompted to enter username/password, keycloak will use the existing session.
So if you want to have different session for the same user, then you have to create your own session, for example if your clients are using apache, you can use apache oidc module to create a local session ( which will be your ClientA session), as for keycloak session, you cant have two sessions, but you can have one keycloak session for the two clients and two apache session for each clients.


Q: C# Please specify the assembly explicitly in the type name I have a umbraco site where i wish to add a fileuploader from a separate working VS project. I have added it to my umbraco VS project with Add existing project.
I have then moved the code from default.aspx to a usercontrol so i could add it to my umbraco.
But i get a 500 error when trying to upload some files. In my event viewer i have the following 2 exceptions:
Exception message: The type 'jQueryUploadTest.FileTransferHandler' is ambiguous: it could come from assembly 'C:\UmbracoSites\sitename\sitename\bin\sitename.DLL' or from assembly 'C:\UmbracoSites\sitename\sitename\bin\jQueryUploadTest.DLL'. Please specify the assembly explicitly in the type name.
The directory specified for caching compressed content C:\Users\name\AppData\Local\Temp\iisexpress\IIS Temporary Compressed Files\Clr4IntegratedAppPool is invalid.  Static compression is being disabled.
What does it mean? How can it come from the sitename.DLL when dll etc are copyed from jQueryUploadTest on build and have nothing to do with sitename.DLL.

A: You surely have the type QueryUploadTest.FileTransferHandler in both assemblies.
Make sure that sitename.dll doesn't have a type name like that or, to make sure this problem won't occur other times, change the namespace of one of the projects.
It's good practice to have diferent namespaces for diferent assemblies, exactly to avoid this kind of problem.


Q: Identify offset of a value in numpy Given a flatten NxN array in numpy, I'd like to find the minimum value, and its offset in the array. I've managed to find the minimum value, but is it possible to identify the offset (which row and which column)?
In the example below, a = 0.5, how can I know if it is 0.5 from [1,0], or [2,1]?
from numpy import *

value = 0
NUM_NODE = 5
EDGE = array(zeros((NUM_NODE, NUM_NODE)))
EDGE = [[ 0.,          0.,          0.,          0.,          0.        ],
    [ 0.5,         0.,          0.,          0.,          0.        ],
    [ 1.,          0.5,         0.,          0.,          0.        ],
    [ 1.41421356,  1.11803399,  1.,          0.,          0.        ],
    [ 1.,          1.11803399,  1.41421356,  1.,          0.        ]]

a = reshape(EDGE, NUM_NODE*NUM_NODE)
print min(filter(lambda x : x > value, a))

A: You could use np.where:
>>> edge = np.array(EDGE)
>>> edge[edge > 0].min()
0.5
>>> np.where(edge == edge[edge > 0].min())
(array([1, 2]), array([0, 1]))

which gives the x coordinates and the y coordinates which hit the minimum value separately.  If you want to combine them, there are lots of ways, e.g.
>>> np.array(np.where(edge == edge[edge > 0].min())).T
array([[1, 0],
       [2, 1]])

A few asides: from numpy import * is a bad habit because that replaces some built-in functions with numpy's versions which work differently, and in some cases have the opposite results; ALLCAPS variable names are usually only given to constants; and your
 EDGE = array(zeros((NUM_NODE, NUM_NODE)))

line doesn't do anything, because your EDGE = [[ 0., ... etc line immediately makes a new list and binds EDGE to it instead.  You made an array and threw it away.  There's also no need to call array here; zeros already returns an array.

A: numpy.ndenumerate will enumerate over the array(by the way, you shouldn't lose position information with reshaping).
In [43]: a = array(EDGE)

In [44]: a
Out[44]:
array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.5       ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 1.        ,  0.5       ,  0.        ,  0.        ,  0.        ],
       [ 1.41421356,  1.11803399,  1.        ,  0.        ,  0.        ],
       [ 1.        ,  1.11803399,  1.41421356,  1.        ,  0.        ]])

In [45]: min((i for i in ndenumerate(a) if i[1] > 0), key=lambda i: i[1])
Out[45]: ((1, 0), 0.5)

Or you can do it with the old way if you want every occurence:
In [11]: m, ms = float("inf"), []

In [12]: for pos, i in ndenumerate(a):
   ....:     if not i: continue
   ....:     if i < m:
   ....:         m, ms = i, [pos]
   ....:     elif i == m:
   ....:         ms.append(pos)
   ....:

In [13]: ms
Out[13]: [(1, 0), (2, 1)]


Q: export c++ classes to DLL, including external DLL So I am trying to export into a dll a class that uses a class from another dll. An example will explain the situation better.
//aclass.h
class  __declspec(dllexport) aclass{
    public:
        void amethod();
};

_
//aclass.cpp
#include "aclass.h"

void aclass::amethod(){
    std::cout<<"a method"<<std::endl;
}

So without including anything external this compiles (aclass.dll) and runs from other projects, ex.
#include <aclass.h>

void main(){
    aclass a;
    a.amethod();

    _getch();
}

The problem arises when I include an external header (that comes with a dll and a lib file, the paths of which are passed to the compiler). As soon as I include the external header:
//aclass.h
#include <externalapi.h>
class  __declspec(dllexport) aclass{
    public
    void amethod();
};

without even calling any class of function from the externalapi, when try to compile I get:
Error 1 error LNK2001: unresolved external symbol __imp__Math_e C:\...\aclass.obj  aclass
Error 1 error LNK2001: unresolved external symbol __imp__Math_pi C:\...\aclass.obj aclass
Error 1 error LNK2001: unresolved external symbol __imp__Math_zero C:\...\aclass.obj aclass
....etc

Originally (without the __declspec(dllexport) directive) I would access these by something like:
Math::pi;
Math::e;
etc.

as they are static constants of the externalapi.
From what I understand on how the whole thing with dll exporting works, this is what is called name mangling(?). So two questions:

*

*What should I change in the syntax so that the function names of the external library are "loaded" with their original c++ names? This has to be somehow possible. Up until now I was developing my code, as a stand-alone application, meaning that I was not using the __declspec(dllexport) keywords, I was including the same header file, using the exact same dll and lib file and everything was compiling and running smoothly. Obviously, the code above is a oversimplification of my actual code to point out the problem.

*In most of the "export to dll" how-to I have found around, people use __declspec(dllexport) and __declspec(dllimport). I understand __declspec(dllexport) more or less tells the compiler to export the part of code to dll, and this makes sense. What exactly is the meaning of __declspec(dllimport). For instance why is this first piece of code, I wrote at the beginning, compiling and usable as a dll without the need of __declspec(dllimport)?

Thanks for your time!

A: __declspec(dllimport) tells the compiler that the function will be imported from a DLL using an import LIB, rather than found in a different OBJ file or a static LIB.
BTW: it sounds like you may not want DLLs at all. DLLs are specifically for swapping out the library after compilation without having to recompile the application, or for sharing large amounts of object code between applications. If all you want is to reuse a set of code between different projects without having to compile it for each one, static libraries are sufficient, and easier to reason about.

A: It's usually best to have a conditional macro that imports or exports depending
#ifdef MODULE1
#define MODULE1_DECL __declspec(dllexport)
#else
#define MODULE1_DECL __declspec(dllimport)
#endif

This way you export functions etc that you want to export, and import what you want to use.
For example see this SO post

You #define MODULE1 (maybe as a setting) in your project that will export definitions, and use the MODULE1_DECL define rather than explicitly putting either __declspec(dllimport) or __declspec(dllexport) in your code. Read the manual for further details.

Name mangling just happens in C++: it indicates namespaces, and the parameters a function overload etc. takes for disambiguation.


Q: What's the best way to have two modules which use functions from one another in Perl? Unfortunately, I'm a totally noob when it comes to creating packages, exporting, etc in Perl. I tried reading some of the modules and often found myself dozing off from the long chapters. It would be helpful if I can find what I need to understand in just one simple  webpage without the need to scroll down. :P
Basically I have two modules, A & B, and A will use some function off from B and B will use some functions off from A. I get a tons of warning about function redefined when I try to compile via perl -c.
Is there a way to do this properly? Or is my design retarded? If so what would be a better way? As the reason I did this is to avoid copy n pasting the other module functions again into this module and renaming them.

A: So... the suggestion to factor out common code into another module is
a good one.  But, you shouldn't name modules *.pl, and you shouldn't
load them by require-ing a certain pathname (as in require
"../lib/foo.pl";).  (For one thing, saying '..' makes your script
depend on being executed from the same working directory every time.
So your script may work when you run it as perl foo.pl, but it won't
work when you run it as perl YourApp/foo.pl.  That is generally not good.)
Let's say your app is called YourApp.  You should build your
application as a set of modules that live in a lib/ directory.  For
example, here is a "Foo" module; its filename is lib/YourApp/Foo.pm.
package YourApp::Foo;
use strict;

sub do_something {
    # code goes here
}

Now, let's say you have a module called "Bar" that depends on "Foo".
You just make lib/YourApp/Bar.pm and say:
package YourApp::Bar;
use strict;
use YourApp::Foo;

sub do_something_else {
    return YourApp::Foo::do_something() + 1;
}

(As an advanced exercise, you can use Sub::Exporter or Exporter to
make use YourApp::Foo install subroutines in the consuming package's
namespace, so that you don't have to write YourApp::Foo:: before
everything.)
Anyway, you build your whole app like this.  Logical pieces of
functionally should be grouped together in modules (or even better,
classes).
To make all this run, you write a small script that looks like this (I
put these in bin/, so let's call it bin/yourapp.pl):
 #!/usr/bin/env perl

 use strict;
 use warnings;
 use feature ':5.10';

 use FindBin qw($Bin);
 use lib "$Bin/../lib";

 use YourApp;
 YourApp::run(@ARGV);

The key here is that none of your code is outside of modules, except a
tiny bit of boilerplate to start your app running.  This is easy to
maintain, and more importantly, it makes it easy to write automated
tests.  Instead of running something from the command-line, you can
just call a function with some values.
Anyway, this is probably off-topic now.  But I think it's important
to know.

A: The simple answer is to not test compile modules with perl -c... use perl -e'use Module'
or perl -e0 -MModule instead.
perl -c is designed for doing a test compile of a script, not a module.  When you run it
on one of your
When recursively using modules, the key point is to make sure anything externally referenced is set up early.  Usually this means at least making use @ISA be set in a compile time construct (in BEGIN{} or via "use parent" or the deprecated "use base") and @EXPORT and friends be set in BEGIN{}.
The basic problem is that if module Foo uses module Bar (which uses Foo), compilation of Foo stops right at that point until Bar is fully compiled and it's mainline code has executed.  Making sure that whatever parts of Foo Bar's compile and run-of-mainline-code
need are there is the answer.
(In many cases, you can sensibly separate out the functionality into more modules and break the recursion.  This is best of all.)

A: It's not really good practice to have circular dependencies.  I'd advise factoring something or another to a third module so you can have A depends on B, A depends on C, B depends on C.


Q: Tkinter OptionMenu arguments I like to explicitly specify arguments. For example:
def func(a, b):
    return a+b

Anytime I call it I write:
func(a=6, b=7)

Instead of:
func(6, 7)

I can't do this with the OptionMenu class in Tkinter. The following example is within a custom class
self.var = tk.StringVav()
choices = ['op1', 'op2']
self.menu_m = tk.OptionMenu(master=self.frame_1, variable=self.var, *choices)

This results in multiple values for the argument 'master'. How can I explicitly define the master, variable, and list of options to use?

A: Unfortunately, the OptionMenu widget is somewhat poorly implemented. Regardless of your preferences, the optionmenu isn't designed to accept keyword arguments for the first three parameters master, variable, and value. They must be presented in that order as positional arguments.
self.menu_m = tk.OptionMenu(self.frame_1, self.var, *choices)


Q: Scrapy for eCommerce with Selenium Infinite Scroll, Help Returning Values I am rather new to programming. I've done a few small projects on my own and have started getting into making webscrapers with Scrapy. I'm trying to make a scraper for Home Depot and have run into issues. The problem this is trying to solve is that the Home Depot webpage has javascript that only loads when you scroll down the page, so I added some code I found that scrolls down the page to reveal all the products so that it can grab the title, review count, and price of each product tile. Before adding this code, it was indeed scraping product info correctly; after adding it I originally had issues with the code only scraping the last page of results, so I moved some things around. I think being new I just don't understand something about objects in Scrapy and how information is passed, particularly the HTML I'm trying to get it to return values for in parse_product. So far this indeed opens up the page and goes to the next page, but it's not scraping any products anymore. Where am I going wrong? I have been struggling with this for hours, I'm taking a class in web scraping and while I've had some success it seems like if I have to do anything slightly off course it's a massive struggle.
import scrapy
import logging
from scrapy.utils.markup import remove_tags
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
from scrapy.selector import Selector
from time import sleep
from datetime import datetime

class HdSpider(scrapy.Spider):
    name = 'hd'
    allowed_domains = ['www.homedepot.com']
    start_urls = ['https://www.homedepot.com/b/Home-Decor-Artificial-Greenery-Artificial-Flowers/N-5yc1vZcf9y?Nao='] #Add %%Nao= to end of URL you got from search or category

    def parse(self, response):
        options = Options()
        chrome_path = which("chromedriver")
        driver = webdriver.Chrome(executable_path=chrome_path)#, chrome_options=options)
        p = 0 # The home depot URLs end in =24, =48 etc basically products are grouped 24 on a page so this is my way of getting the next page
        start_url = 'https://www.homedepot.com/b/Home-Decor-Artificial-Greenery-Artificial-Flowers/N-5yc1vZcf9y?Nao='

        while p < 25:
            driver.get(start_url + str(p))
            driver.set_window_size(1920, 1080)
            #sleep(2)
            scroll_pause_time = 1
            screen_height = driver.execute_script("return window.screen.height;")   # get the screen height of the web
            i = 1

            while True: #this is the infinite scoll thing which reveals all javascript generated product tiles
                driver.execute_script("window.scrollTo(0, {screen_height}*{i});".format(screen_height=screen_height, i=i))
                i += 1
                sleep(scroll_pause_time)
                scroll_height = driver.execute_script("return document.body.scrollHeight;")
                if (screen_height) * i > scroll_height:
                    break
                self.html = driver.page_source
            p = p + 24

    def parse_product(self, response):
        resp = Selector(text=self.html)
        for products in resp.xpath("//div[@class='product-pod--padding']"):
            date = datetime.now().strftime("%m-%d-%y")
            brand = products.xpath("normalize-space(.//span[@class='product-pod__title__brand--bold']/text())").get()
            title = products.xpath("normalize-space(.//span[@class='product-pod__title__product']/text())").get()
            link = products.xpath(".//div//a//@href").get()
            model = products.xpath("normalize-space(.//div[@class='product-pod__model'][2]/text())").get()
            review_count = products.xpath("normalize-space(.//span[@class='product-pod__ratings-count']/text())").get()
            price = products.xpath("normalize-space(.//div[@class='price-format__main-price']//span[2]/text())").get()
            yield {
               'Date scraped' : date,
               'Brand' : brand,
               'Title' : title,
               'Product Link' : "https://www.homedepot.com" + remove_tags(link),
               'Price' : "$" + price,
               'Model #' : model,
               'Review Count' : review_count
            }

A: I don't see where you runs parse_product. It will not execute it automatically for you. Besides function like your parse_product with response is rather to use it in some yield Requests(supage_url, parse_product) to parse data from subpage, not from page which you get in parse. You should rather move code from parse_product into parse like this:
def parse(self, response):
    options = Options()
    chrome_path = which("chromedriver")
    driver = webdriver.Chrome(executable_path=chrome_path)#, chrome_options=options)
    driver.set_window_size(1920, 1080)

    p = 0 # The home depot URLs end in =24, =48 etc basically products are grouped 24 on a page so this is my way of getting the next page

    start_url = 'https://www.homedepot.com/b/Home-Decor-Artificial-Greenery-Artificial-Flowers/N-5yc1vZcf9y?Nao='

    scroll_pause_time = 1
    screen_height = driver.execute_script("return window.screen.height;")   # get the screen height of the web

    while p < 25:
        driver.get(start_url + str(p))

        #sleep(2)
        i = 1

        # scrolling
        while True: #this is the infinite scoll thing which reveals all javascript generated product tiles
            driver.execute_script("window.scrollTo(0, {screen_height}*{i});".format(screen_height=screen_height, i=i))
            i += 1
            sleep(scroll_pause_time)
            scroll_height = driver.execute_script("return document.body.scrollHeight;")
            if (screen_height) * i > scroll_height:
                break

        # after scrolling
        self.html = driver.page_source
        p = p + 24
        resp = Selector(text=self.html)
        for products in resp.xpath("//div[@class='product-pod--padding']"):
            date = datetime.now().strftime("%m-%d-%y")
            brand = products.xpath("normalize-space(.//span[@class='product-pod__title__brand--bold']/text())").get()
            title = products.xpath("normalize-space(.//span[@class='product-pod__title__product']/text())").get()
            link = products.xpath(".//div//a//@href").get()
            model = products.xpath("normalize-space(.//div[@class='product-pod__model'][2]/text())").get()
            review_count = products.xpath("normalize-space(.//span[@class='product-pod__ratings-count']/text())").get()
            price = products.xpath("normalize-space(.//div[@class='price-format__main-price']//span[2]/text())").get()
            yield {
               'Date scraped' : date,
               'Brand' : brand,
               'Title' : title,
               'Product Link' : "https://www.homedepot.com" + remove_tags(link),
               'Price' : "$" + price,
               'Model #' : model,
               'Review Count' : review_count
            }

But I would do other changes - you use p = p + 24 but when I check page in browser then I see I need p = p + 48 to get all product. Instead of p = p + ... I would rather use Selenium to click button > to get next page.

EDIT:
My version with other changes.
Everyone can run it without creating project.
#!/usr/bin/env python3

import scrapy
from scrapy.utils.markup import remove_tags
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
from scrapy.selector import Selector
from time import sleep
from datetime import datetime

class HdSpider(scrapy.Spider):

    name = 'hd'

    allowed_domains = ['www.homedepot.com']
    start_urls = ['https://www.homedepot.com/b/Home-Decor-Artificial-Greenery-Artificial-Flowers/N-5yc1vZcf9y?Nao='] #Add %%Nao= to end of URL you got from search or category

    def parse(self, response):

        options = Options()

        chrome_path = which("chromedriver")
        driver = webdriver.Chrome(executable_path=chrome_path) #, chrome_options=options)
        #driver.set_window_size(1920, 1080)
        print(dir(driver))
        driver.maximize_window()

        scroll_pause_time = 1

        # loading first page
        start_url = 'https://www.homedepot.com/b/Home-Decor-Artificial-Greenery-Artificial-Flowers/N-5yc1vZcf9y?Nao=0'
        driver.get(start_url)

        screen_height = driver.execute_script("return window.screen.height;")   # get the screen height of the web

        #while True:        # all pages
        for _ in range(5):  # only 5 pages

            #sleep(scroll_pause_time)

            # scrolling page
            i = 1
            while True: #this is the infinite scoll thing which reveals all javascript generated product tiles
                driver.execute_script(f"window.scrollBy(0, {screen_height});")
                sleep(scroll_pause_time)

                i += 1

                scroll_height = driver.execute_script("return document.body.scrollHeight;")
                if screen_height * i > scroll_height:
                    break

            # after scrolling
            resp = Selector(text=driver.page_source)

            for products in resp.xpath("//div[@class='product-pod--padding']"):
                date = datetime.now().strftime("%m-%d-%y")
                brand = products.xpath("normalize-space(.//span[@class='product-pod__title__brand--bold']/text())").get()
                title = products.xpath("normalize-space(.//span[@class='product-pod__title__product']/text())").get()
                link = products.xpath(".//div//a//@href").get()
                model = products.xpath("normalize-space(.//div[@class='product-pod__model'][2]/text())").get()
                review_count = products.xpath("normalize-space(.//span[@class='product-pod__ratings-count']/text())").get()
                price = products.xpath("normalize-space(.//div[@class='price-format__main-price']//span[2]/text())").get()
                yield {
                   'Date scraped' : date,
                   'Brand' : brand,
                   'Title' : title,
                   'Product Link' : "https://www.homedepot.com" + remove_tags(link),
                   'Price' : "$" + price,
                   'Model #' : model,
                   'Review Count' : review_count
                }

            # click button `>` to load next page
            try:
                driver.find_element_by_xpath('//a[@aria-label="Next"]').click()
            except:
                break

# --- run without project and save in `output.csv` ---

from scrapy.crawler import CrawlerProcess

c = CrawlerProcess({
    'USER_AGENT': 'Mozilla/5.0',
    # save in file CSV, JSON or XML
    'FEEDS': {'output.csv': {'format': 'csv'}},  # new in 2.1
})

c.crawl(HdSpider)
c.start()


Q: homebrew Formula to install package As part of new joinees, we provide the FAQ for running internal homebrew formula, that installs from zip/tar.gz files, we install using " brew tap ..." . However, we are separately running "brew install mariadb@10.2" as well as running "brew tap...". Question -  wanted to consolidate all brew commands into one using formula, i.e i wanted to have ability to install for this package name  "mariadb@10.2" into TAP formula we have.
Please share the information if any.
Let me know if not clear.

A: You can run brew install mariadb@10.2 && brew tap ..., but you can’t combine brew install mariadb@10.2 and brew tap ... into one brew command.
However, running brew install on a formula from a tap you don’t have automatically taps the latter:
brew install org/tap/thing

Is equivalent to:
brew tap org/tap
brew install org/tap/thing

Where org/tap is the GitHub repository https://github.com/org/tap.
This means that if you want to install mariadb@10.2 as well as some other formula from that tap, you can run a command like this:
brew install mariadb@10.2 org/firsttimesetup/xyz

Which is equivalent to:
brew tap org/firsttimesetup
brew install mariadb@10.2 org/firsttimesetup/xyz


Q: Unable to connect to PayPal Sandbox to verify message received by IPN listener I am testing my IPN listener using the IPN simulator. After sending IPN on the IPN simulator, my log shows that my IPN listener receives the IPN but when it sends back the message with cmd=_notify-validate added to it, the sandbox does not respond. The error shown on the IPN simulator is "IPN Delivery Failed:500 Internal Server Error". The following error is met after my listener sends out the message to sandbox: "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 173.0.82.77:443".
When I test my listener with the live PayPal URL, it responds with the "INVALID" message - which shows that my listener is working fine.
After checking with my web hosting company, they advised that the PayPal Sandbox IP was not blocked and that the problem should lie with PayPal sandbox. Please advise if it is possible that PayPal has blocked the source IP and hence is not responding to it. If so, how can I go about unblocking the source IP? Any help is appreciated. Thanks.
I am using sample ASP.NET C# code provided by PayPal for my IPN listener and my log stops at "Sending request to PayPal...".
SSLogger.logger.Info("IPN Invoked");

//Post back to either sandbox or live
string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
string strLive = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);

//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(Request.ContentLength);   //get request values
string strRequest = "cmd=_notify-validate&" + Encoding.ASCII.GetString(param);

//set request values
req.ContentLength = strRequest.Length;
SSLogger.logger.Info("sb: " + Encoding.ASCII.GetString(param));
SSLogger.logger.Info("strRequest: " + strRequest);

//Send the request to PayPal and get the response
SSLogger.logger.Info("Sending request to PayPal...");
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
SSLogger.logger.Info("Request sent out to PayPal.");
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
SSLogger.logger.Info("Response from PayPal received.");

SSLogger.logger.Info(strResponse);

A: Try this instead:
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;


Q: Can I make large rightBarButtonItem in Swift? What I have:

self.title = "Title"
navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItem?.tintColor = .white

Is it possible to make this Add looks exactly like this Title?
Something like this:

Or any other solution for this - title left - button with image right, same heights?

A: please go through this answer which shows how you can manage Navigation bar title when collapsed and when it's large. It won't give you an exact answer but it will definitely help you in achieving what you want.
Other then that please go through this answer which will help you understand how give x,y positions to right bar button item.
Just a quick overview how you can achieve what you want by the combination of this two answers:-

*

*Set the position of the right bar button item according to the position or height of the navigation bar which you wanted to do. Link number 2 will help you in doing that.

*Observe your navigation bar when the height of your navigation bar is increased or decreased you can change the position of your right bar button item.

and Done. By using this you can manage the position of your bar button item at both the item when your Navigation bar is Enlarged at that time you can show button at different position and when the Navigation Bar is collapsed you can show button at different position so that it doesn't look vice-versa of your question after changing position of your button.

A: I've done something similar in my app - not exactly what you are looking for, but should give you enough to go on:
    func setupNavBar() {
    let rightButton = UIButton()
    rightButton.setTitle("Leave", for: .normal)
    rightButton.addTarget(self, action: #selector(rightButtonTapped(button:)), for: .touchUpInside)
    navigationController?.navigationBar.addSubview(rightButton)
    rightButton.tag = 97
    rightButton.frame = CGRect(x: self.view.frame.width, y: 0, width: 120, height: 20)

    let targetView = self.navigationController?.navigationBar
    let trailingConstraint = NSLayoutConstraint(item: rightButton, attribute:
        .trailingMargin, relatedBy: .equal, toItem: targetView,
                         attribute: .trailingMargin, multiplier: 1.0, constant: -16)
    let bottomConstraint = NSLayoutConstraint(item: rightButton, attribute: .bottom, relatedBy: .equal,
                                              toItem: targetView, attribute: .bottom, multiplier: 1.0, constant: -6)
    rightButton.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([trailingConstraint, bottomConstraint])
    }

I also created this function to remove it (hence the tag use above):
    func removeRightButton(){
        guard let subviews = self.navigationController?.navigationBar.subviews else{
            log.info("Attempt to remove right button but subviews don't exist")
            return
        }
        for view in subviews{
            if view.tag == 97 {
                view.removeFromSuperview()
            }
        }
    }


Q: Working with ampersands in $_GET functions If I have a URL like asdf.com/index.php?a=0&b=2, then using $_GET for a would be 0 and for b would be 2. However, the term I put into a single $_GET function has an ampersand in it already, like a=Steak&Cheese. Is there a way to make ampersands work without the $_GET variable thinking its job ends when the ampersand shows up (therefore not pulling the entire term)?

A: urlencode() it so & turns into %26.
If you need to make a query string out of some parameters, you can use http_build_query() instead and it will URL encode your parameters for you.
On the receiving end, your $_GET values will be decoded for you by PHP, so the query string a=Steak%26Cheese corresponds to $_GET = array('a' => 'Steak&Cheese').

A: Yes, you must URL Encode before request URL. Read this http://www.w3schools.com/TAGS/ref_urlencode.asp

A: Here is a previous post covering this in jquery AJAX requests, but to summarize you have to encoded the uri. This will convert the ampersand value to a ascii value.
Ampersand in GET, PHP


Q: Asp.Net Core 5 Docker container exit (1) I'm trying to run a docker container for my api application.  The application is asp.net mvc (.net core) 5.0.  I'm publishing the application to a /publish folder like:
dotnet publish -c Release -o ./publish

The Dockerfile contains:
FROM mcr.microsoft.com/dotnet/sdk:5.0
WORKDIR /app
COPY publish/ app/
EXPOSE 80
ENTRYPOINT ["dotnet", "AmaraCode.RainMaker.DataService.dll"]

To create the image I'm using:
docker build -t amaracodellc/dataservice .

To create the container I'm using:
docker run --name dataservice_rm --network="rm" --ip 192.168.0.40 -p 7601:80 -d amaracodellc/dataservice:latest

The container creates but exits immediately with Exit (1).  The log says:
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET program, but dotnet-AmaraCode.RainMaker.DataService.dll does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

I'm a bit stumped because the assembly name is correct since I copied it from the screen versus typing it in.  There are many messages on the web for Exit (1) but I can't seem to find one that helps in this case.
It is worth noting that I'm VERY new to Docker so I'm hoping someone here can see something that I'm not seeing.
Thanks in advance
Scott


Q: windows 2008 server how to get ASPEmail to send to own domain I have a friend who runs his website on a windows 2008 server. I have set up an asp classic form to email page on his website (hisdomain.com) which takes the input from a contact form and sends it out by email.
The problem is that it only works if the email is being sent to a different domain (something@anotherdomain.com). If the email is being sent to the same domain (anything@hisdomain.com) it never arrives.
Mailer.AddAddress "someone@anotherdomain.com"

Works.
Mailer.AddAddress "someone@hisdomain.com"

Does not work.
I have hunted this site and Google for a resolution but cannot find one.
Does anyone know how to fix this issue?
Many thanks
Tog Porter

A: Update: It turns out he uses Gmail business to control his domain emails and there was a filter in there that bounced the messages because the sender was the same as the recipient.
Bypassing the Gmail spam filter has fixed the problem.

A: Use this code and replace your smtp server information

<%

Set myMail=CreateObject("CDO.Message")
myMail.BodyPart.Charset = "UTF-8"

myMail.Subject= Your Message Subject
myMail.From= "anotheremail@anotherdomain.com"
myMail.To=Receiver Email Address
myMail.CreateMHTMLBody "Test Email Subject"

myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2

myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")= SMTP_SERVER
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")=SMTP_Email_Username
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")=Smtp_Email_Password
myMail.Configuration.Fields.Update
myMail.Send
set myMail=nothing

%>


Q: How to compile OpenGL with a python C++ extension using distutils on Mac OSX? When I try it I get:

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/cscalelib.so, 2): Symbol not found: _glBindFramebufferEXT
    Referenced from: /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/cscalelib.so
    Expected in: dynamic lookup

I've tried all sort of things in the setup.py file. What do I actually need to put in it to link to OpenGL properly? My code compiles fine so there's no point putting that on there. Here is setup.py
from distutils.core import setup, Extension

module1 = Extension('cscalelib',
              extra_compile_args = ["-framework OpenGL", "-lm", "-lGL", "-lGLU"],
                    sources = ['cscalelib.cpp'])

setup (name = 'cscalelib',
       version = '0.1',
       description = 'Test for setup_framebuffer',
       ext_modules = [module1])

A: I didn't realise I had to remove the build directory. Now it imports correctly.
For anyone that needs to know you need: extra_link_args=['-framework', 'OpenGL'] Delete the build directory and try it again. It will work.


Q: Ansible consume AD command output i got a problem how to consume the following command output in Ansible
basically im trying to get the list of Active Directory OUs and loop over that list to search for specific name. My script works well when multiple OUs exists but i have an issue when only single OU exists. Explained below
    tasks:
  - name: PS - Pull System OUs from AD
    win_command: powershell -
    args:
      stdin: "Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchScope 1  -SearchBase 'OU=SYSTEMS,DC=domain,DC=int' -Server domain.int | select-object name | ConvertTo-json"
    become: yes
    register: ou_reg_out

  - name: Select Systems OU
    block:
    - name: Set list of standardized OUs as facts
      set_fact:
        ou_reg_list: "{{ ou_reg_out.stdout  }}"

    - name: Set System OU for system
      set_fact:
        ou_name: "OU={{item.name}}"
      loop: "{{ ou_reg_list }}"
      when: (item.name|upper) == (srv_type|upper)
    when: ou_reg_out.stdout|length != 0

basically i need to be able to loop over the ou_reg_out.stdout.
It works when command returns multiple OUs as ou_reg_out.stdout returns list:
ou_reg_out.stdout:
  - { name: OU1 }
  - { name: OU2 }

issue is when only single OU exists , command doesnt return the list
ou_reg_out.stdout:
  { name: OU1 }

Any idea how to workaround this problem ?

A: Test the type of the variable and branch the code.
json_query filter helps to select the items from the list. Then ternary helps to conditionally select the value. The value of the first item that matches the condition is used. Defaults to 'NOTFOUND'.
For example the play bellow for both versions of ou_reg_list
- hosts: localhost

  vars:
    ou_reg_list:
      - { name: OU1 }
      - { name: OU2 }
#   ou_reg_list:
#     { name: OU1 }
    srv_type: 'ou1'

  tasks:
    - set_fact:
        ou_name: "OU={{ (ou_reg_list.name == srv_type|upper)|
                        ternary( ou_reg_list.name, 'NOTFOUND') }}"
      when: ou_reg_list is mapping

    - block:

        - set_fact:
            ou_names: "{{ ou_reg_list|json_query(query) }}"
          vars:
            query: "[?name=='{{ srv_type|upper }}'].name"

        - set_fact:
            ou_name: "OU={{ (ou_names|length > 0)|
                            ternary( ou_names.0, 'NOTFOUND') }}"
      when: ou_reg_list is not mapping

    - debug:
        var: ou_name

gives
"ou_name": "OU=OU1"


Q: C# XmlSerializer Deserialize Date format I need to bring (among others) a date from an XML file to a PostgreSQL table in my C# application.
My problem is that if I declare the field as a string like this:
[XmlAttribute("startDate")]
public string StartDate { get; set; }

it doesn't get deserialized at all (the value is null). If I declare it as DateTime like this:
[XmlAttribute("startDate")]
public DateTime StartDate { get; set; }

no matter what I enter into the XML field I always get the value 01.01.0001 00:00:00.
I tried to enter the date using YYYY-MM-DD, YYYY/MM/DD and DD.MM.YYYY. What am I doing wrong? It works perfectly fine for other strings and integers.
Edit:
Example XML:
<?xml version="1.0" encoding="utf-8" ?>
<command name="TestCommand">
  <weeks>11</weeks> <!-- this works fine -->
  <startDate>2017/02/01</startDate> <!-- this doesn't -->
</command>

Deserialization happens using XmlSerializer.Deserialize() into an Config file which consists of the fields I gave examples of above

A: Ok the answer is simple. This is not XmlAttribute ... this is XmlElement.
Change attribute to:
[XmlElement("startDate")]
public DateTime StartDate { get; set; }

Are you sure element "weeks" works properly and is marked with XmlAttribute ?


Q: Pod OOM - Xmx not beeing respected I am starting a Java app as follows
JAVA_OPTS=-Xmx7g -Xms512m -Xss1m -XX:+UseCompressedOops -XX:-OmitStackTraceInFastThrow

The pod I am using for this, has
        resources:
          limits:
            cpu: 4096m
            memory: 9Gi
          requests:
            cpu: 1024m
            memory: 512Mi

However, I see once and then, pods being killed with
"Memory cgroup out of memory: Kill process 3284727 (java) score 1982 or sacrifice child
Killed process 3284727 (java) total-vm:15318320kB, anon-rss:9380388kB, file-rss:20180kB, shmem-rss:0kB"

Why does this take place?
How come the memory usage surpasses 9G given that I set Xmx=7G

A: With -Xmx you only specify the Java heap size - there is a lot of other memory that the JVM uses (like stack, native memory for the JVM, direct buffers, etc.).
In our experience the correct size for total usage of the JVM is 1.5 to 2 times the heap size but this depends heavily on your use case (for example some applications using direct buffers may have 32GB of RAM using only 1GB of heap).
So run your app with more limits, check the actual usage and then define that + 5% as a container limit.
You should also adapt your request to at least 1Gi with -Xms512m.


Q: Duplicated metrics using prometheus federation What happened?
The duplicated metrics appear on main prometheus server:
namespace_workload_pod:kube_pod_owner:relabel{cluster="sample-eks", job="federate", namespace="cert-manager", pod="cert-manager-848f547974-xkg8j", prometheus="monitoring/k8s", prometheus_replica="prometheus-k8s-0", workload="cert-manager", workload_type="deployment"}

namespace_workload_pod:kube_pod_owner:relabel{cluster="sample-eks", namespace="cert-manager", pod="cert-manager-848f547974-xkg8j", workload="cert-manager", workload_type="deployment"}

The difference is additional job,prometheus and prometheus_replica  labels.
Did you expect to see some different?
No duplicated metrics.
How to reproduce it (as minimally and precisely as possible):
Install two kube-prometheus stacks on different clusters:

*

*one main

*one child

The main cluster will get the data from the child cluster using /federate endpoint.
Federation config:
- job_name: 'federate'
  scrape_interval: 15s
  honor_labels: true
  metrics_path: '/federate'
  params:
    'match[]':
      - '{job=~".*"}'
  static_configs:
  - targets:
    - 'prometheus-sample.dev.xyz.com'

Environment
I use last git tag of kube-prometheus repo on AWS EKS 1.21.

*

*Prometheus Logs:

level=warn ts=2021-10-01T15:04:21.300Z caller=scrape.go:1399 component="scrape manager" scrape_pool=federate target="http://prometheus-sample.dev.sample.com:80/federate?match%5B%5D=%7Bjob%3D~%22.%2A%22%7D" msg="Error on ingesting samples with different value but same timestamp" num_dropped=20

A: The best option here is to deploy all prometheus servers with the option replicaExternalLabelName: ""
More info here:
https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#prometheusspec


Q: FLutter check if Textfield controller is empty i have a question in flutter about a Textfield and the controller attribute.
I have a site add_new_currency_screen.dart,
for the beginning i have textfields like:
name, amount, price, startDate and endDate.
Now my problem.
The endDate is always "empty" if i open the "add screen", if i enter a startDate the Enddate should be added by a year.
 TextFormField(
                      decoration: InputDecoration(labelText: 'EndDate'),
                      controller: _endDateController..text = DateFormat("dd.MM.yyyy").format(_pickedStartDate.add(Duration(days: 365))),
                      enabled: false,
                    ),

The problem is im getting an error "add" was called on "null"
in controller i can't use if else,
and _endDateController.text == null ? "empty" : DateFormat("dd.MM.yyyy").format(_pickedStartDate.add(Duration(days: 365))), does not work to....
How can i check or how can i fix this, that if the startDate is picked, the controller in the endDate is doing the thing, else, just dont use the "add"
hope i could explain it correctly and someone can help me :)
thanks all,
best regards
Thommy

A: If you get "add" was called on null, then the problem has to do with _pickedStartDate
So perhaps try something like:
controller: _endDateController..text = _pickedStartDate != null ? DateFormat("dd.MM.yyyy").format(_pickedStartDate.add(Duration(days: 365))) : '',


Q: Spring WebFlux How to get Flux execute result? I want to use WebClient in Spring WebFlux to call some urls, and then put all the monos to flux. when I call Flux.blockLast, I can not get the results.
    @Test
    public void reactiveGetTest() {
        long start = System.currentTimeMillis();
        List<String> results = new ArrayList<>();
        List<Mono<String>> monos = IntStream.range(0, 500)
                .boxed()
                .map(i -> reactiveGet("https://www.google.com/"))
                .collect(Collectors.toList());
        Flux.mergeSequential(monos)
                .map(results::add)
                .blockLast();
        System.out.println("result: " + results.size());
        System.out.println("total time: " + (System.currentTimeMillis() - start));
    }

    private Mono<String> reactiveGet(String url) {
        return WebClient.create(url)
                .get()
                .retrieve()
                .bodyToMono(String.class);
    }

I want to get a list of size 500, but was 0!

A: You can use Flux.collectList() to get all results in a list:
@Test
public void reactiveGetTest() {
    long start = System.currentTimeMillis();
    List<Mono<String>> monos = IntStream.range(0, 500)
            .boxed()
            .map(i -> reactiveGet("https://www.google.com/"))
            .collect(Collectors.toList());
    List<String> results = Flux.mergeSequential(monos).collectList().block();
    System.out.println("result: " + results.size());
    System.out.println("total time: " + (System.currentTimeMillis() - start));
}


Q: What are the prohibited numbers in android dialer app? Learning the source code of Android Dialpad Fragment I've noticed quite strange thing - you can not call several numbers.
if (number != null && !TextUtils.isEmpty(mProhibitedPhoneNumberRegexp)
    && number.matches(mProhibitedPhoneNumberRegexp)) {
    Log.i(TAG, "The phone number is prohibited explicitly by a rule.");
if (getActivity() != null) {
    DialogFragment dialogFragment =
    ErrorDialogFragment.newInstance(
        R.string.dialog_phone_call_prohibited_message);
    dialogFragment.show(getFragmentManager(), "phone_prohibited_dialog");
}

where the mProhibitedPhoneNumberRegexp should be loaded as
mProhibitedPhoneNumberRegexp = getResources().getString(
    R.string.config_prohibited_phone_number_regexp);

But I can't find the related string in dailer module and not in the PhoneCommen module and not in the Contacts module of the Android source. So my question is where to find this String, why Android OS prohibits to dial some numbers, is it White House or something?

A: You can find the default in this file:
https://android.googlesource.com/platform/packages/apps/Dialer/+/master/res/values/donottranslate_config.xml?autodive=0%2F%2F
As you can see, it is empty.


Q: Python Regex with split Method I am trying to create a list from a csv file. However, I'm having a tough time using the split method because some of the attributes in the csv file has comma's that are within parenthesis. for example:
csv file:

500,403,34,"hello there, this attribute has a comma in it",567

So for example, when I iterate through the file:
for line in f:
    fields = line.split(",")

fields = ['500','403','34','"hello there','this attribute has a comma in it"','567']

How can I get it to look like this:
fields = ['500','403','34','"hello there, this attribute has a comma in it"','567']

I would like to use Regex for this, but if there is an easier way I'd love to hear it. Thanks!

A: import re
x='500,403,34,"hello there, this attribute has a comma in it",567'
print re.split(r""",(?=(?:[^"]*"[^"]*"[^"]*)*[^"]*$)""",x)

Output : ['500', '403', '34', '"hello there, this attribute has a comma in it"', '567']

A: Just use the existing CSV package. Example:
import csv
with open('file.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile)
        for row in reader:
            print ', '.join(row)

A: The CSV module is the easiest way to go:
import csv

with open('input.csv') as f:
    for row in csv.reader(f):
        print row

For input input.csv:

500,403,34,"hello there, this attribute has a comma in it",567
500,403,34,"hello there this attribute has no comma in it",567
500,403,34,"hello there, this attribute has multiple commas, in, it",567

The output is:

['500', '403', '34', 'hello there, this attribute has a comma in it', '567']
['500', '403', '34', 'hello there this attribute has no comma in it', '567']
['500', '403', '34', 'hello there, this attribute has multiple commas, in, it', '567']


Q: autoincrement in a mysql update query String textbox1=request.getParameter("textbox1");
String textbox2=request.getParameter("textbox2");
String textbox3=request.getParameter("textbox3");
String textbox4=request.getParameter("textbox4");
String textbox5=request.getParameter("textbox5");
String textbox6=request.getParameter("textbox6");
String textbox7=request.getParameter("textbox7");
String textbox8=request.getParameter("textbox8");
String textbox9=request.getParameter("textbox9");
String textbox10=request.getParameter("textbox10");
String textbox11=request.getParameter("textbox11");
String textbox12=request.getParameter("textbox12");

for(int i=1;i<13;i++){
    String textbox=request.getParameter("textbox"+i+"");
    st.executeUpdate("update user_start2 set data='"+textbox+"'");
}

I have a table with column data,name and id.
I want to update the table with the above query,but the table gets populated with only the last value.
Is it possible to auto-increement as I am updating? so that the first data goes to the first user,2nd data to 2nd user and so on in the table..
When I system out I am able to retrieve values from the jsp to my servlet

A: I do not know how/where you are using this, but bear in mind that it is completely vulnerable to SQL injection attacks.
String textbox1=request.getParameter("textbox1");
String textbox2=request.getParameter("textbox2");
String textbox3=request.getParameter("textbox3");
String textbox4=request.getParameter("textbox4");
String textbox5=request.getParameter("textbox5");
String textbox6=request.getParameter("textbox6");
String textbox7=request.getParameter("textbox7");
String textbox8=request.getParameter("textbox8");
String textbox9=request.getParameter("textbox9");
String textbox10=request.getParameter("textbox10");
String textbox11=request.getParameter("textbox11");
String textbox12=request.getParameter("textbox12");

for(int i=1;i<13;i++){
    String textbox=request.getParameter("textbox"+i+"");
    st.executeUpdate("update user_start2 set data='"+textbox+"' where id="+i+";");
}

A: I assume that id is a unique identifier in your table user_start2.  If so, then you need to alter the update statement above so that it reads,
"update user_start2 set data= '"+textbox+"' where id = '"+Myuserid);
Based on what you are saying, I assume that there are 13 entries in your database and that you are updating each one.   If so, there must be some correspondence between the ids and the index in your for loop.   If it is a literal 1 to 1 mapping, then you can simply write your update statement as
"update user_start2 set data= '"+textbox+"' where id = '"+i+"'");

If there is some transformation that needs to be done first, you could create a temporary variable and set the id to that:
For example
     int temp = i+4;
"update user_start2 set data= '"+textbox+"' where id = '"+temp+"'");

A: Use PreparedStatement it prevents SQL Injection in Java
for(int i=1;i<13;i++){
 String textbox=request.getParameter("textbox"+i+"");
 PreparedStatement ps = con.prepareStatement("update user_start2 set data=? where id=?");

 //set the values of ?(place holders)
 ps.setString(1,textbox);
 ps.setInt(2,i); //assuming id is i.

 ps.executeUpdate();
}

See also

*

*Oracle docs tutorial PreparedStatement


Q: Can't figure out why angular binding won't work in codepen script I'm trying to use angular in codepen, which it seems to support, but I can't bind to my controller's $scope object for some reason.  I've tried a couple different versions of angular via cdn and there are other pens I've seen that are able to successfully use angular.  Can anyone tell me why my implementation isn't working?  Here is the pen. the version of angular used is 1.4.0.
Here is the html code:

        {{2 + 2}}

        {{'cat'}}

        {{$scope.cat}}

and here is the JS code:
var App = angular.module("App", [])
  .controller("catControl", function($scope) {
    $scope.cat = 'cat';
  });

Thanks.

A: You need learn more about $scope. We never user {{$scope.key}} in view. Instead we we use just {{key}} in View
Your code should be

        {{2 + 2}}

        {{'cat'}}

        {{$scope.cat}}
        {{cat}}

learn more about scope at This link
& This Link


Q: Populating parameter list with a query I have a custom big query that functions correctly, but I would like to add a parameter to that query. I would have expected to be able to populate the drop down list with another query but cannot see how to do it. Is this possible?

A: You cannot populate a BigQuery query parameter dropdown list using another BigQuery query.
A workaround for this would be:

*

*create a Community Connector using Advanced Services

*Add the query parameter as a config param

*in getConfig, use BigQuery REST API or Apps Script BigQuery service to retrieve list

*in getData, pass the parameter from your getConfig to BigQuery