Can this code, to convert string to integer, be made more compact?

Styling

Try to make the code follow the PEP 8 Style Guidelines, this will make your code follow the Python conventions, and also more readable. So do
if l == 1 instead of if l==1, my_atoi instead of myAtoi, etc

Code structure

It seems that page requires the code to be inside a class. Leaving the exercise aside for a moment:
You don't need that function inside a class, because you are never using anything of the class itself. It makes more sense to define that method outside the class, and without the (unused) self parameter.

Skiping whitespaces

while(i<l and s[i]==" "):
    i+=1
    if i==l:
        return 0

You don't need those parenthesis on the while in Python.
Your code might not detect some whitespace characters like \t (it seems the exercise accepts that though); use the method isspace().
Instead of checking i == l on every iteration (when it could only happen in the last one), check it once you are out of the loop. This allows us to remove the special treatment to empty strings: if s is empty, it won't enter the loop and i == l, so it will return 0.

while i < l and s[i].isspace():
    i += 1

if i == l:
    return 0

Sign

Your code to handle the sign is too complicated. Instead of keeping ispos and isnegative, you could just keep a factor that is either 1 or -1. So instead of

ispos = False
if(s[i]=="+"):
    ispos = True
    i+=1
isnegative = False
if(s[i]=='-' and not ispos):
    isnegative = True
    i+=1

you can do

factor = -1 if s[i] == '-' else 1
# Skip the sign character if necessary
if s[i] == '-' or s[i] == '+':
    i += 1

Reading the number

if(i<len(s) and not s[i].isnumeric() and s[i]!='+' and s[i]!='-'):
    return 0
while(i<l):
    if s[i].isnumeric():
        res += s[i]
    else:
        break
    i+=1

If we have already parsed the sign, why accept another sign again?
Be careful, isnumeric() accepts things like ½, which you probably don't want; use isdigit() instead.
EDIT: As noted in the comments, isdigit() will accept things like "¹". So it's probably better to just do s[i] in '0123456789'
It's good practice (and helps avoid unexpected bugs) to declare variables the closest possible to where they are used. You declare res = "" at the beginning of your function, but haven't used it until now.
The while and if can be condensed into a single, more compact loop:

res = ""
while i < l and s[i].isdigit():
    res += s[i]
    i += 1

# You can check if an string is empty doing this:
if not res:
    return 0

Arrived to this point, if the function hasn't returned, we know res contains a number. The way you handle the valid range, before applying the sign, is not very intuitive; it's better if you first apply the sign.

x = factor * int(res)
if x > (1 << 31):
    return (1<<31)
elif x < -((1 << 31) - 1):
    return (1 << 31) - 1
else:
    return x

Or it might be even better to clamp your number using min and max:

return max(-(1 << 31) + 1, min(1 << 31, factor * int(res)))

Final code

I have removed the special case of l == 1 because it is already handled by the rest of the code. This is my final code (untested):

def my_atoi(s: str) -> int:
    l = len(s)

    while i < l and s[i].isspace():
        i += 1

    if i == l:
        return 0

    factor = -1 if s[i] == '-' else 1
    # Skip the sign character if necessary
    if s[i] == '-' or s[i] == '+':
        i += 1

    res = ""
    while i < l and s[i].isdigit():
        res += s[i]
        i += 1

    if not res:
        return 0

    return max(-(1 << 31) + 1, min(1 << 31, factor * int(res)))


Angular - reusing an observable for different queries

There's nothing (obviously) wrong with your code. You deal with observables the efficient way, IMO. Cache is good. The fact you're constructing a "base" observable in constructor without subscribing to it there is good as well...

There are only a few things I can point out:

Design: Be careful with caching, if your user information changes often, the cache may bite you back. You may want to implement some kind of cache invalidation policy.
Design: If your system has thousands of users, you may want to not load all of them into memory at once. That would require your API support pagination though. If the users are few, it's totally okay to do what you're doing, I think.
RxJs: not sure why pipe() and tap() are used the way are used. I think, it's easier to use do() and map() as shown in the code below. Chaining looks much more readable (as "step-by-step" explanation of how the data is transformed into the desired result).
Idioms: TypeScript is all about types. Do specify a known type when possible instead of using any. E.g. Observable<IUserDetails[]> is better than Observable<any[]>.
Style: Variable naming is important. Do not use one-letter names (u), they are pure evil. Names like data are also evil, in your case it's better to call it response. The resource$ can probably be nicer to the reader if called userDetails$. You get the idea.

@Injectable()
export class UsersApiService {

  private readonly baseUrl: string = 'https://reqres.in/api/users';
  userDetails$: Observable<IUserDetails[]>;

  constructor(private http: HttpClient) {
    this.userDetails$ = this.http
      .get<IUserDetails[]>(this.baseUrl)
      .do(response => console.log('"getUsers" successfully called!'))
      .map(response => response.data)
      .publishReplay(1)
      .refCount();
  }

  getUsers(): Observable<IUser[]> {
    return this.userDetails$
      .map(userDetailsList =>
        userDetailsList.map(userDetails =>
          <IUser>{ id: userDetails.id, name: `${userDetails.first_name} ${userDetails.last_name}` }
        )
      );
  }

  getUserById(id: number): Observable<IUserDetails> {
    return this.userDetails$
      .map(userDetailsList => <IUserDetails>userDetailsList.find(userDetails => userDetails.id === id));
  }
}


Suggestions for better oop designs with real example (file services)

Protected Field vs Getter and setter

I prefer to not access fields directly, because it's difficult to use and not very flexible.

When I need to implement a new fileservice, I just need to know that I need to define a field called fileExtension. If you do not thoroughly document this, implementing new services in the future will be a pain (and even with documentation it's not ideal, as nobody really reads it anyways).

The second problem is that I can only define a string as file extension. What if I want to create an image file service, that allows the uploading of jpg and png files? I would need to dynamically set fileExtension, and the only place I could do that is in saveToDB or setNewFileNameIfExist, neither of which should really be responsible (and even though it may work right now, it may easily break once you change your base FileServices class).

It would be a lot better to just add an abstract getter for the file extension.

The same problem exists with path as well, and also works the other way around: in PDF you work directly on fileName, instead of using some setter method.

Field location

It is odd that you store the filename in FileServices, but the fileExtension and path in PDF. Just put them all in FileServices to simplify things.

File class

You may also consider creating a File class, which could hold file, fileName and fileExtension, as they all belong together. It may also contain common functions such as the one to get a filename (without extension).

Various Store Methods

I think that it is not ideal to have saveToDB and saveToServer in your base service. What if I only want to save the file in the database, but not the server?

Instead, I would have a generic, abstract storeFile function in FileServices.

Need for abstract FileServices?

Of course, if you follow the advise above, it's not clear that FileServices is actually needed as an abstract class. The only thing it does is provide a function to get the name of a file (which may as well be a utility function in some file utility class, as it is likely used elsewhere as well), and a function to save a file to the server (which is really just a call to a different function.)

If you expect more common functionality in the future, this isn't really a problem, otherwise you may consider changing the abstract class to an interface.

Misc

the PDF class isn't really a PDF, but a specific kind of file service, so PDFFileService would be a better name.
I prefer constructors to not do the actual work. It seems unclean, and the resulting calling code looks wrong. Reading new PDF($file), I would assume that a new object was created, but not that anything else was actually done. Something like new PDF($file)->store() would read more logical to me.
Your variable names are not consistent with what you consider real names. In saveToServer it's actually the new file name, in setNewFileNameIfExist, it's the original file name.


There's a lot to be improved here.  What you call dividers would be simply called primes.  This may have loosely been inspired by a sieve, but its inner mechanics are nothing like a sieve.  It's no surprise that performance degrades with larger numbers, and if you go too high, you may run into memory issues since the dividers (or primes) keeps a list in memory.

Performance should also suffer because you check every number from 2 onward.  You could check 2 to eliminate all evens first, and then only check odd numbers after that.

We discourage one line calls here unless enclosed in braces.  So change this:

if (divider * divider > inputValue)
    return false;

To this:

if (divider * divider > inputValue)
{
    return false;
}

You may want to investigate using a sieve.  Here are old examples:

Sieve31, my sieve of Eratosthenes returning IEnumerable<int>

Sieve32, a simple 32 bit sieve returning IEnumerable<uint> using C#

Sieve32FastV2 - A fast parallel Sieve of Eratosthenes


Since the SQL query is just a fixed string, there is no chance for SQL injection.

However, the way you print the output is a concern, because you fail to escape it for HTML.  For example, if one of the subsubcat_names contains a < character, then it might be interpreted as an HTML tag.

In the worst case, if subsubcat_name contains a malicious string like <script>while (true) alert("Ha ha");</script>, it would be executed by the browser as JavaScript.  Cross-site scripting attacks like that can be used, for example, to steal login credentials.

To prevent such accidental or malicious breakage, you should call htmlspecialchars():

echo htmlspecialchars($row['subsubcat_name']);

Even if the subsubcat_names are not under user control, it's still good programming practice to ensure correctness by escaping all HTML output as HTML.


Helper class to change property of string when serializing for Json.Net

Just a quick point, regarding readability; the switch block would be better off written like this:

//remove "."
var titleRemoveDots = title.Replace(".", "");
var titleLowerCase = titleRemoveDots.Trim().ToLower();
switch(titleLowerCase)
{
    case "dir":
        return 1;
    case "dr":
    case "doctor":
        return 4;
    case "mag":
    case "magister":
        return 5;
    case "ing":
        return 6;
    case "dipling":
    case "dipl":
        return 7;
    case "prof":
    case "professor":
        return 8;
    case "dkfm":
        return 9;
    case "prok":
        return 12;
    default:
        return null;
}

The absence of break; between cases (/ the presence of return in all cases) can make it harder to refactor the switch block later. I think it would be better to separate the concept of figuring out the return value and that of returning a value:

int? result;

//remove "."
var titleRemoveDots = title.Replace(".", "");
var titleLowerCase = titleRemoveDots.Trim().ToLower();
switch(titleLowerCase)
{
    case "dir":
        result = 1;
        break;
    case "dr":
    case "doctor":
        result = 4;
        break;
    case "mag":
    case "magister":
        result = 5;
        break;
    case "ing":
        result = 6;
        break;
    case "dipling":
    case "dipl":
        result = 7;
        break;
    case "prof":
    case "professor":
        result = 8;
        break;
    case "dkfm":
        result = 9;
        break;
    case "prok":
        result = 12;
        break;
    default:
        break;
}

return result;

Now, call me a switch-hater, I'd probably end up with something like this:

public enum PersonTitle
{
    Director = 1,
    Doctor = 4,
    Magister = 5,
    Ingineer = 6,
    Dipling = 7,
    Professor = 8,
    Dkfm = 9, // todo: give readable name
    Prok = 12 // todo: give readable name
}

private static _titleIds = new Dictionary<string, PersonTitle> {
                             { "dir", PersonTitle.Director },
                             { "dr", PersonTitle.Doctor },
                             { "doctor", PersonTitle.Doctor },
                             { "mag", PersonTitle.Magister },
                             { "magister", PersonTitle.Magister },
                             { "ing", PersonTitle.Ingineer },
                             { "dipl", PersonTitle.Dipling },
                             { "dipling", PersonTitle.Dipling },
                             { "prof", PersonTitle.Professor },
                             { "professor", PersonTitle.Professor },
                             { "dkfm", PersonTitle.Dkfm },
                             { "prok", PersonTitle.Prok }
                         };

And then GetTitleId could look like this:

public static int? GetTitleId(string title)
{
    var cleanTitle = title.Replace(".", string.Empty).Trim().ToLower();
    PersonTitle result;

    if (_titleIds.TryGetValue(cleanTitle, out result))
    {
        return (int)result;
    }
    else
    {
        return null;
    }
}

One issue is that Title is actually a string representation of the int value, which can be allowed to be null - I might be mistaken, but it looks like the setter for Title would throw a NullReferenceException if/when that is the case, because it's calling ToString() on null:

[JsonProperty(PropertyName = "9")]
public string Title
{
    get
    {
        return _title;
    }
    set
    {
        _title = value;
        _title = GetTitleId(Title).ToString();
    }
}

I'd expect a null-check there:

 set
 {
     _title = value;
     _title = (GetTitleId(Title) ?? string.Empty).ToString();
 }


First you need to read Schneier's Memo to the Amateur Cipher Designer.  All amateur cryptography is faulty, not just yours.  I too designed a cryptographic hash, based round RC4, put it out on the web and Scott Fluhrer was kind enough to show me the obvious faults in it.  He is an expert, so he knew what to look for.  I was not and I did not know what I had to protect against.  Our conversation is probably still out there on the sci.crypt Usenet group from 2007.

To design a good hash function you need to know the types of attacks that are used against hashes.  For example, does your hash specifically guard against length-extension attacks?

How fast is your hash compared to other current cryptographic hashes such as SHA3?

For a first suggestion, learn how cryptographic hashes pad their input and why.  Either incorporate the same technique into your hash or else find a different way to serve the same purpose.  You are not the first person to write a hash.  Learn so you can stand on the shoulders of giants.


Pretty print with unit and physical suffix II

The computation of postdecimal can be simplified to postdecimal = max(precision - predecimal, 0).

eng_format needs a docstring.

On self.seq:

It needs a meaningful name like examples.
It should be local to test_eng_format, since that's the only place it's used.
It can be built in one expression, without mutation:

examples = [(0.002777, 'V', 3, '2.78 mV'),
            (0.02777, 'V', 3, '27.8 mV'),
            ...]

However, looping over a list of test cases is less readable than a bunch of self.assertEqual(eng_format(-0.1, 'A', 6), '-100.000 mA') calls.

test_eng_format should not print all its test cases. Tests should be silent unless they fail.


This is unnecessary:

if s == []:
    return [s]

You can safely remove it, the program will still work.

This step is not so great:

if subset not in sets:
    sets.append(subset)

For two reasons:

Checking if a list contains some items is an \$O(n)\$ operation
Comparing sets is not free either

A more efficient solution is to count from 0 until 1 << n, and use the bitmap of the count to decide the elements that are part of a subset.

def subsets(s):
    sets = []
    for i in range(1 << len(s)):
        subset = [s[bit] for bit in range(len(s)) if is_bit_set(i, bit)]
        sets.append(subset)
    return sets

def is_bit_set(num, bit):
    return num & (1 << bit) > 0

That is, in a subset, each element may be either present, or not. So each element has only 2 possible states: in or out. 1 or 0. If we look at the binary representation of numbers from 0 to 2^n -1, where n is the number of elements, for example when n=3, we have:

    cba
0 = 000
1 = 001
2 = 010
3 = 011
4 = 100
5 = 101
6 = 110
7 = 111

There are 8 possible subsets, and the bits represent whether an element is in the subset or not.
This is the idea used by the program:

The outer loop goes from 0 until 2^n - 1.
The inner loop goes from 0 until n - 1.
1 << bit is 1 shifted to the left bit times.

For example, when i = 3, that corresponds to bits 011.
We loop from 0 until 2, comparing i against 001, 010, and 100.
For these values, the expression i & (1 << bit) will be evaluated as
011 & 001 = 001, 011 & 010 = 010, and 011 & 100 = 000, respectively.
The first two are greater than 0, the last one is not.


If it's just ASCII, treat it as a byte array all the way until the end. Vec has a swap method and you can reconstruct a String from the bytes.
Use map and collect instead of allocating a Vec and manually pushing.
There's no need to allocate a String to pass to &str.

fn transposes(s: &str) -> Vec<String> {
    let bytes = s.as_bytes();

    (1..bytes.len()).map(|i| {
        let mut transpose = bytes.to_owned();
        transpose.swap(i - 1, i);
        String::from_utf8(transpose).expect("Invalid UTF-8")
    }).collect()
}

fn main() {
    println!("{:?}", transposes("maxi"));
}

Even better, if your data is ASCII, encode that in the type system:

extern crate ascii;

use ascii::{AsciiStr, AsciiString};

fn transposes(s: &AsciiStr) -> Vec<AsciiString> {
    (1..s.len()).map(|i| {
        let mut transpose = s.to_owned();
        transpose.as_mut_slice().swap(i - 1, i);
        transpose
    }).collect()
}

fn main() {
    let s = AsciiStr::from_ascii("maxi").expect("Not ASCII");
    println!("{:?}", transposes(s));
}

Note that the transposes function can no longer fail due to invalid strings.


One producer and multiple consumers wherein the producer has to wait until all consumers finish before adding more data

It's not recommended to inherit from Thread. Instead implement Runnable and pass it to a new Thread which you store in a member and provide accesses for what you need. This also lets you pass a ThreadFactory for some customization options for the threads used.

class Producer extends Runnable{

    private Thread thr;

    Producer(Queue queue, ThreadFactory fact) {
        if(fact == null)
            thr = new Thread(this, "producer thread");
        else
            thr = fact.newThread(this);
        //...
    }

    public void start(){
        thr.start();
    }

    //...
}

As for synchronizing if you only needed the consumers to run once you could have used a CountDownLatch but that cannot be reset. So you'll have to use a CyclicBarrier.

With a cyclic barrier you can make the producer the Runnable that gets invoked when the last thread calls its await. Then you can be sure all but one consumer threads are waiting for the producer code to finish.


Here are a few things that came to my mind:

Bug

if temp != ' ' will not work when there are more than 1 consecutive spaces.
To fix this, use if not temp.isspace() instead of comparing with a hard-coded string.
For example, s = 'a,   [b]' will output ['a', ['b'], '   '] for your current code.

Your code outputs ['a', ' b'] for a, b. I'll assume that including the space is a feature and not a bug.

Design

Wrap the test code inside  if __name__ == '__main__'. This will prevent the code from being called when being imported from another module.

Function names should preferably be lowercase. Change the CamelCase names to snake_case.

In return statements, you need not enclose the items in a parenthesis if you are returning a tuple

result = list() can be replaced with just result = []

if len(temp) can be replaced with just if temp. The bool of empty values are False in python.

res = parse_to_list(string, i + 1)
i = res[1]
result.append(res[0])

The above can be a bit simplified and can be made a bit more understandable.
nested_list, i = parse_to_list(string, i + 1)
result.append(nested_list)

Instead of using string[i], you can declare a new element char which is equal to string[i]
(This is just my personal preference)

You can declare parse_to_list to inside list_parse. This will remove the need to pass string inside a recursion repeatedly, and will also make the inner function "private".
(But this is also just my personal preference)

The final code should look something like this after applying the above:
def list_parse(string):
    def parse_to_list(cont=0):
        result = []
        temp = ''
        i = cont

        while i < len(string):
            char = string[i]

            if char == ',':
                if temp and not temp.isspace():
                    result.append(temp)
                temp = ''

            elif char == '[':
                nested_list, i = parse_to_list(i + 1)
                result.append(nested_list)

            elif char == ']':
                if temp and not temp.isspace():
                    result.append(temp)
                return result, i

            else:
                temp += char

            i += 1

        if temp and not temp.isspace():
            result.append(temp)

        return result, i

    return parse_to_list()[0]

if __name__ == '__main__':
    s = 'a,s,[c,f],[f,t], [[a,c],[d3,32]]'

    print(list_parse(s))


It's mostly minor issues, but since you want nitpicking... here we go:

In your public method you check the sanity of the input for null. Assuming you want to check for sanity, then you should check also that none of the sub-arrays is null, and that they all have the same size. But that becomes a bit tedious to do in a "constructor", so you'd better extract it into a "checkArgument" method.
Stop using i and j with the matrix. Row and Col force you to reason more about their meaning, and will prevent you from silly mistakes where you switch them (though this is my personal preference)
Your method is called "int checkAdjacentsForBombs", but if it's an int it would be better named "int countAdjacentsBombs"
While I see their convenience, I don't like your "checkNorth"... style of returning ints. I'd rather have it being "isCellABomb(char[][], int row, int col) and then you can generalize all methods in a single one and do:

if (isCellABomb(matrix, row-1, col)) count++;

if (isCellABomb(matrix, row-1, col+1)) count++; ....

This will allow you to reuse it also in the buildSweepMatrix method check

You pass the matrix around everywhere, bc it's in the signature of your methods. But those are all private, so you may ease it up a bit by storing the matrix in the class and sharing it, since no one else will access those methods, though it's debatable.
You should really extract your bomb marker ('*') to a final static const BOMB_SYMBOL, to ease readability and maintainability (so if tomorrow you mark bombs by % instead, you need to change in a single place
You first do the "costly" operation of counting the bombs, and then checking if it was needed here:

int count = checkAdjacentsForBombs(matrix, i, j);

if (matrix[i][j] != '*') matrix[i][j] = (char) (count + '0');

Try doing the opposite so you only compute stuff when needed.


Calculating unknown parts of a right triangle

Keep it simple

        numOfSides=0;
        numOfAngles=0;

        sideA=a; if(sideA>0){numOfSides++;}
        sideB=b; if(sideB>0){numOfSides++;}
        sideC=c; if(sideC>0){numOfSides++;}

        alpha=A; if(alpha>0){numOfAngles++;}
        beta=B; if(beta>0){numOfAngles++;}

        if(numOfAngles<2)
        {
            findMissingAngles();
        }

        if(numOfSides<3)
        {
            findMissingSides();
        }

This seems more complicated than necessary.  Why have numOfAngles and numOfSides at all?  You could just say

        sideA = a;
        sideB = b;
        sideC = c;

        alpha = A;
        beta = B;

        findMissingAngles();

        findMissingSides();

And then modify

        if(numOfAngles==1)

in calcAlpha to say

        if (beta > 0)

That saves two object fields that exist just for construction purposes.

Be careful of false modularity

    private void calcBeta()
    {
        beta=90-alpha;
    }

This only works if you calculate alpha first.  That's the programming logic here, but nothing enforces it.  So why have a separate method to do an assignment based on one subtraction?  You could change

        if(beta==0)
        {
            calcBeta();
        }

to say

        if (beta==0)
        {
            beta = 90 - alpha;
        }

instead.  Then you could get rid of calcBeta.

This also avoids the problem of calling calcBeta without testing that beta doesn't already have a value.

Alternately, you could change calcBeta to be more like calcAlpha where it calculates alpha from beta if beta exists or from two sides otherwise.

Similarly, all the calcSide methods assume that alpha exists.  And calcSideB and calcSideC assume that sideA exists.  If any of that is not true, then you'll get weird results.

Exceptional circumstances

    private boolean isPossible()
    {
        return ((alpha+beta+90)==180 && Math.abs(Math.sqrt(Math.pow(sideA, 2) + Math.pow(sideB, 2)) - sideC) < 1);
    }

Why not change (alpha+beta+90)==180 to just (alpha + beta) == 90?

Why is 1 the acceptable error?  Why not .1 or .03 or something else?

Are negative values acceptable?  Because you accept them if they have the right magnitude.  For example, -95 and 185 are a valid pair of angles.  And -3, -4, 5 are valid side values.


Running a MySQL query with an admin class

@Max already did a good job at describing how you could write better code.

Here is what is wrong with your current approach:

The main problem is that your classes are tightly coupled. All the files you posted need to be aware that you are using PDO, which isn't good (it's hard to read and maintain).

Your admin class also doesn't make any sense. It doesn't contain code that is specifically concerned with anything related to admins, it just contains part of the database access code (but for some odd reason not all of it).

So what I would do now is:

keep all the PDO calls in one place. There really isn't a good reason to have prepare in one place, and execute and fetch in a completely different place.
remove the generic runQuery method from admin, as it's not specific to admins.
move the admin select query into a method called getById inside the admin class.

Doing this, you basically arrive at the code @Max posted.

Misc

Class names should be upper-case.
Don't echo in classes, it makes them difficult to reuse. Just throw the exception upwards and deal with it later.
The way connect is written, you would use a new connection for each object. For example, if you not only had an admin class, but also a SomeData class, it would also call connect, creating a new connection. It's better to reuse the same connection across the request. So either pass the connection to the admin constructor, or to the getById method.
Try to be consistent with your names. Examples are admin_session vs admin_id, auth_admin vs admin, or conn vs db. These inconsistencies do decrease readability, as a reader has to think about them each time.
Either use camelCase or snake_case, don't mix them.
Your spacing is off.


To me it seems like you are trying to make a async task for the sake of making it async to fit in with.

  I'm trying to make my ASP.net Web API methods asynchronous

I don't see any reason for these pieces of code to be async.  They aren't doing long running tasks and aren't even hitting the db (unless Add doesn't something we don't know about of course like saves as well which is a different point of review right there).

result = await Task.Run(() =>_RepositoryUser.Add<DTOUser, DTOUser>(user));

//use the login object to my 2nd task
var task2 = await Task.Run(() =>_RepositoryLogIn.Add<DTOLogin, DTOLogin>(login));

Once you have removed these, the method becomes a truly synchronous method.  As it's a pretty trivial method anyway this shouldn't be a problem and making it async only confuses the issue for no benefit.


1. Follow the styleguide (PEP8)

don't import modules which you're not using (random)
more, imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
I always like to put my imports in alphabetical order, so that it gets easier to find them.
you should have two newlines before defining your methods
you should also have a space after every ,
you should put a space before and after operators (x = 'something'); this is not required when defining arguments.
instead of commenting code in the middle of the method, try to split the logic of your program in separate functions.
you should be using the new .format() style

2. Working on the code:

you should really let the user choose where the files actually are
don't use bare excepts.
you can directly use .encode() if you want to convert a string to bytes. That's actually the pythonic way of doing it as per this SO discussion
when reading a file, you can omit the r mode, as that's the default one

3. Efficiency
The problem does not come from the zip module but rather from the cartesian product you're doing. As @jonsharpe mentioned in his comments, your program has to go through a lot of passwords.
The final code would look like this:
"""
Zipfile password cracker using a dictionary attack and, if not successful,
switches to bruteforce
"""

import itertools
import string
import time
import zipfile

ARCHIVE_PATH = 'test.zip'
DICTIONARY_PATH = 'dictionary.txt'

def dictionary_attack():
    """Tries at first a dictionary attack"""

    t0 = time.time()
    with open(DICTIONARY_PATH) as f:
        for password in f:
            password = password.rstrip().encode()
            try:
                zipfile.ZipFile(ARCHIVE_PATH).extractall(pwd=password)
                t1 = time.time()
                print('Password found: {}\nTime spent: {} seconds'.format(password.decode(), t1 - t0))

                return True
            except RuntimeError:
                pass
    return False

def bruteforce_attack(nbcharmax):
    """If the password hasn't been found yet, the function switches to bruteforce"""

    alphabet = string.ascii_letters + string.digits + string.punctuation

    t0 = time.time()
    for i in range(1, nbcharmax):
        for j in itertools.product(alphabet, repeat=i):
            password = ''.join(j).encode()
            try:
                zipfile.ZipFile(ARCHIVE_PATH).extractall(pwd=password)
                t1 = time.time()
                print('Password found: {}\nTime spent: {} seconds'.format(password.decode(), t1 - t0))

                return True
            except RuntimeError:
                pass
    return False

if __name__ == "__main__":
    if not dictionary_attack():
        bruteforce_attack(4)


script that imports only data that is not in the database yet

You don't specify how many new records you're adding. But you did mention in comments some trouble with the "database getting locked", which I'll interpret as "database bogging down because you were hammering it with this script", and so I'll assume you're inserting a lot of records.

If you're doing that, and you're not stuck on an old version of Django, there are some bulk data commands to help you. More on that later.

First, though, let's talk about your constraints. You are applying a very strict uniqueness constraint to company names: a company name is assumed to be totally distinct.

I don't know your context, but I'm from the United States. In the US, corporations are managed at the state level: you'll hear things like "ExampleCorp, a Delaware corporation, ..."

Also, businesses other than corporations are managed at the county or city level (depending on the state, and the size of the city). So I can go down to my "county seat" (the town where the business of the county is conducted, usually containing the jail and the county courthouse) and register a "DBA form" (DBA means "doing business as") that creates a "name" for my business.

So you might go to your county seat in county 1, and register "TomCo". And someone else named Tom might go to the county seat in county 2 and register "TomCo". And they would be totally legit.

So, again, I don't know your use case. But you might need to re-think the whole "company names are distinct" thing. Perhaps you could attach an email domain, which is guaranteed to be globally unique, to the company record?

Regardless, there's also the chance that your batch of users might include the same company multiple times. For example, if your Django app is some sort of SaaS and you are bulk-loading a new customer, maybe all the new people are from the same company. So I think it behooves you to cache that information.

Next, there's the chance that your users are in the input file more than once. So you might as well filter those out.

Next, there's the concept that your users must have a "first name" and a "last name" and that's it. Seriously? That's very Anglo-centric, but not very realistic. What about people from Brazil, who generally go by a single name, like "Ronaldinho"? (Although they come from the Portuguese tradition, so they might have 4 names if they get formal: "Ronaldo de Assis Moreira"?) What about my all-time favorite bandit, "Tuco Benedicto Pacifico Juan Maria Ramirez"? At least your database uses a single name field, so that's good.

And finally, you should process the new user creations in bulk if possible, to minimize the number of database calls and the number of transactions.

Let's take a look:

def handle(self, *args, **options):
    with open('/path/to/myFile.csv') as f:
        reader = csv.reader(f)
        next(f)

The only problem with the first few lines is the hard-coding of the csv file. Is there no way to get that into a command line argument, or configuration setting?

        for row in reader:
            company_in_list = row[0].strip()        ###reading the company name from the file
            visitor_company = ''
            company_in_db = Company.objects.filter(name=company_in_list)        ###searching for this company name in the db
            time.sleep(0.05)

This section I object to because you're doing things in a scattershot order. There are several places where you pull out fields from the csv row, and you always use the all the fields, except when you raise an exception and abort. So let's put all the fields together:

for row in reader:
    company = row[0].strip()
    email   = row[1].strip()
    first   = row[2].strip()
    last    = row[3].strip()

But we can do better! That's just looping over a bunch of things, performing the exact same computation for each: we could use a list comprehension or a generator expression, like so:

for row in reader:
    (co_name, email, first, last) = (field.strip() for field in row)

Your CSV file may actually have more columns than 4, so you might have to use row[:4] instead.

First, let's deal with the name. And let's support mononymy! I'm doing this because an invalid name seems like a good reason to skip the record, which we should do as early as possible:

    name = last if not first
        else first if not last
        else first + ' ' + last

    if not name:
        print("Skipping bad names first={!r}, last={!r}".format(first, last))

Next, you check if the company is already in the database. If it's in the database, great. If it's not in the database, you create it. If it's in the database more than once, you raise an exception.

I don't think that last case is really the job of this script. That condition - that the company name should be unique in the table - is properly the job of a database constraint rather than a "create users" script. (It's not even a "create companies" script, which would at least be on-topic.)

So I'm going to suggest you ignore that case. Let's just have the script either create the company if it doesn't exist, or use the existing company if there is already a record.

One thing you don't do, is check if the company name is empty. I don't know if this is a valid case or not, so I'm going to leave that alone (after bringing it to your attention).

Our first step will be to separate out the company names (because there could be M:N of them versus the people), and the email addresses (because that is your key, apparently).

Let's put the company names into a set, in order to make them unique. And we'll put the entire record into a dictionary keyed by the email addresses:

companies = set()
visitors = {} # email -> tuple

for row in ...

    # ...

    companies.add(co_name)

    v = (email, name, co_name)

    if email in visitors:
        print('''Duplicate emails found:
{email} -> {old}
and
{email} -> {new}'''
            .format(email=email, old=visitors[email], new=v))

     visitors[email] = v

At this point, after looping through all the records, you will have a set of company names and a dictionary of visitors, with all the duplicate company names merged in the set, and all the duplicate emails merged in the dictionary.

Now you can process your companies as a single query:

existing_companies = Company.objects.filter(name__in=companies)

That should be a single database call.

You'll want to compute the set of companies that are not in the existing_companies result set, and then bulk_create them:

new_companies = ...

Company.objects.bulk_create([Company(name=cn) for cn in new_companies])

That should be a single database call.

Finally, you have to deal with the users. You existing code doesn't seem to allow for changing jobs: if you have Fred.Flintstone@yahoo.com working at Slate Rock and Gravel, there's no way for him to change jobs to Spacely Sprockets. Trying to change the company associated with a visitor's email will result in an error. I don't know if that's because of what you're doing, or if it's an oversight. You'll have to make the call.

At any rate, you can do a similar thing with querying for email addresses already in the Person table, computing the set of emails not in the table, and doing a bulk_create on those records. (You'll want to create a dictionary to map company names to records, first, using the existing_companies results, plus maybe another query after the bulk create, or a rerun of the whole companies query.)

Again, that should be a single database call. But this depends on your underlying database. The Django docs warn that there are limits on SQLite, so you may have to do your bulk creates in batches.

Summary

I believe you can improve your database availability, and the script performance, by doing the following:

Read all the CSV records into memory (companies, visitors). Do some basic consistency checking during this part.
Make a single bulk query for existing companies.
Perform a single bulk create for new companies.
Refresh the companies query.
Perform a bulk query for existing visitors.
Perform a bulk create for new visitors.

That's potentially only 5 database calls.


PDO (MySQL) connection and query class, safety and logic

1) When using PDO, you want to get the benefits of automatic parameter escaping.
By wrapping your PDO class you are limiting it's functionality. Check out this question for a better example:

https://stackoverflow.com/questions/6366661/php-pdo-parameterised-query-for-mysql

2) Every time you run a query you are making a new PDO instance. It might be better to have an application resource pool that you can call to get a preconfigured db handle.

3) You wrote some sql to be able to fetch a stock by ID, that should probably be functionality that is reusable.

combining 1 & 2 & 3 .. code would probably look better as something like this:

class ApplicationResourcePool
{
    static var $_dbHandle;

    private static $_dbConfig = array(
        'dsn'      => '',
        'username' => '',
        'password' => '',

    );
    public static getDbHandle(){
        if(self::$_dbHandle == null){
            self::$_dbHandle = new PDO(
                self::$_dbConfig['dsn'] ,
                self::$_dbConfig['username'],
                self::$_dbConfig['password']
            );
        }
        return self::$_dbHandle;
    }

}

class StockMapper
{
    protected $_dbh;
    public __construct($dbh = null)
    {
        if($dbh == null){
            $this->_dbh = ApplicationResourcePool::getDbHandle();
        } else {
            $this->_dbh = $dbh;
        }

    }
    public getStockById($stockId){

        $sth=$this->_dbh->prepare("SELECT * from stock WHERE id = :stockId");
        $sth->bindParam(":stockId", $stockId);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        return $result[0];
    }
}

your codeBlock then becomes:

$stockMapper = new StockMapper();
$stockData = $stockMapper->getStockById('302');


First I want to give you a few general comments on the style and aesthetics of your code, then I'll provide you with a detailed code sample applying a slightly different approach to the problem, that you can use as comparison or baseline.

Avoiding unnecessary dependencies

You don't need <Windows.h> for this program. It is the only thing preventing it from compiling on Mac or Linux. You can remove that include file.

<time.h> is also not necessary. You are not currently using any timing routines, that can also be stripped. I also don't see any uses of functions defined in <algorithm>.

Indenting

Is very important to denote nesting. Make sure to always add one level of indentation for code that lives inside a function. The contents of main should be indented to convey that nesting.

Use of "magic numbers"

There are quite a few literal constants in your code that are not clear about their meaning from a first glance. In particular, the several comparisons in the main while loop seem very obscure. I can spot that some of them are actually testing for collision with a trap or the end of the map. You should avoid using raw literal numbers and strings in your program when they can be replaced by named values. We can use const and enum in C++ to define immutable values that have a descriptive name that is much easier to remember than the number 15 or whatever.

But ultimately, for the checks that you're performing, I think you are going about it the wrong way. Instead of testing specific indexes in the map grid, you should just verify what is the symbol in the map for the current position of the player. By doing so, you can get rid of all those magic numbers. You will see in the following sample that I do so and also use constants and enums to name some of the other literal values.

Use a 2D matrix to represent the grid

You've opted for a std::string to represent the map grid, which is fine, but complicates things a little, since the
characters are stored linearly in the string. A more straightforward way of defining the map would be with an actual square matrix of N rows by M columns (or width x height). Like so:

char world_map[MapWidth][MapHeight];

Where the X direction is the width (from the top-left corner to the top-right corner), and Y is the height (from the top-left corner to the bottom-left):

(x=0, y=0)
     +----------+
     |0000000000|
     |0000000000|
     |0000000000|
     |0000000000|
     |0000000000|
     |0000000000|
     |0000000000|
     +----------+ (x=width-1, y=height-1)

Also note that we can use a constant for the width (columns) and height (rows) of the map. This allows us to easily change the size of the map without having to hunt down all over the code for occurrences of literal numbers.

Avoid repetition

Your code has some degree of repetition in the bodies of the if tests. The code for moving the player and checking if she has hit a trap, wall or exit can be generalized into a function that you can call once for each direction of
movement.

In the following example you will see that the movement and map update logic has been moved into a move_player() function, which returns a value indicating if we are free to KeepLooking for the exit or if we have HitTrap and died or FoundExit and are home free.

if (key == Right)
{
    gameState = move_player(world_map, playerPosX, playerPosY, playerPosX + 1, playerPosY);
}
else if (key == Left)
{
    gameState = move_player(world_map, playerPosX, playerPosY, playerPosX - 1, playerPosY);
}
else if (key == Up)
{
    gameState = move_player(world_map, playerPosX, playerPosY, playerPosX, playerPosY - 1);
}
else if (key == Down)
{
    gameState = move_player(world_map, playerPosX, playerPosY, playerPosX, playerPosY + 1);
}
else if (key == Quit)
{
    gameState = QuitGame;
}

Note that the current position of the player in the map it taken by reference (&) so we can modify it inside the function and have the result visible outside in main (see the code below). This is necessary to ensure the player doesn't move outside the map bounds. I don't think you performed that correctly
in your attempt.

Use functions to separate unrelated logic and reuse code

Sometimes it is helpful to split unrelated logic into separate functions. Notice that I have introduced clear_map() and print_map(). This cleans up main and also makes those functions reusable if you decide the expand the code and find yourself needing to clear the map several times, for instance.

Printing the map is already done in two places, so that by itself already warrants it a function.

Putting it all together:

Following is my suggested solution to the exercise, applying the above discussed points. I hope you find it readable, but feel free to ask if you are in doubt about something. I've also added more message printing and a check for invalid keys, so it is slightly longer than your original code.

#include <iostream>
#include <string>

const int MapWidth      = 10;
const int MapHeight     = 7;

// These make our lives easier if we ever decide to change the symbols:
const char PlayerSymbol = 'G';
const char TrapSymbol   = 'T';
const char ExitSymbol   = 'X';
const char EmptySymbol  = '0';

enum Keys
{
    Right = 'd',
    Left  = 'a',
    Up    = 'w',
    Down  = 's',
    Quit  = 'q'
};

enum GameState
{
    HitTrap,
    FoundExit,
    KeepLooking,
    QuitGame
};

void print_map(const char map[MapWidth][MapHeight])
{
    for (int y = 0; y < MapHeight; ++y)
    {
        for (int x = 0; x < MapWidth; ++x)
        {
            std::cout << map[x][y];
        }
        std::cout << "\n";
    }
}

void clear_map(char map[MapWidth][MapHeight])
{
    for (int y = 0; y < MapHeight; ++y)
    {
        for (int x = 0; x < MapWidth; ++x)
        {
            map[x][y] = EmptySymbol;
        }
    }
}

void place_props(char map[MapWidth][MapHeight])
{
    // The initial player position (0,0):
    map[0][0] = PlayerSymbol;

    // Traps:
    map[4][1] = TrapSymbol;
    map[2][3] = TrapSymbol;
    map[1][4] = TrapSymbol;
    map[1][6] = TrapSymbol;

    // Exit is at the right end of the map.
    map[MapWidth - 1][MapHeight - 1] = ExitSymbol;
}

GameState move_player(char map[MapWidth][MapHeight],
                      int & currPlayerPosX, int & currPlayerPosY,
                      int desiredPlayerX,   int desiredPlayerY)
{
    // Clamp the new desired position to stay within the map bounds:
    if (desiredPlayerX < 0) { desiredPlayerX = 0; }
    if (desiredPlayerY < 0) { desiredPlayerY = 0; }
    if (desiredPlayerX >= MapWidth)  { desiredPlayerX = MapWidth  - 1; }
    if (desiredPlayerY >= MapHeight) { desiredPlayerY = MapHeight - 1; }

    GameState newState;

    // Check victory condition or if we hit a trap:
    if (map[desiredPlayerX][desiredPlayerY] == TrapSymbol)
    {
        std::cout << "You hit a trap an got decapitated! Better luck next time.\n";
        newState = HitTrap;
    }
    else if (map[desiredPlayerX][desiredPlayerY] == ExitSymbol)
    {
        std::cout << "You find the exit and live another day to tell the tales of your adventures!\n";
        newState = FoundExit;
    }
    else
    {
        newState = KeepLooking;
    }

    // Clear current position:
    map[currPlayerPosX][currPlayerPosY] = EmptySymbol;

    // Place the player symbol:
    map[desiredPlayerX][desiredPlayerY] = PlayerSymbol;

    // Return the new clamped position, avoiding the player from leaving the map.
    // Since we are using a reference to int, this change will be visible outside.
    currPlayerPosX = desiredPlayerX;
    currPlayerPosY = desiredPlayerY;
    return newState;
}

int main()
{
    int playerPosX = 0;
    int playerPosY = 0;
    GameState gameState = KeepLooking;
    char world_map[MapWidth][MapHeight];

    clear_map(world_map);
    place_props(world_map);
    print_map(world_map);

    while (gameState == KeepLooking)
    {
        char key;
        std::cin >> key;

        if (key == Right)
        {
            gameState = move_player(world_map, playerPosX, playerPosY, playerPosX + 1, playerPosY);
        }
        else if (key == Left)
        {
            gameState = move_player(world_map, playerPosX, playerPosY, playerPosX - 1, playerPosY);
        }
        else if (key == Up)
        {
            gameState = move_player(world_map, playerPosX, playerPosY, playerPosX, playerPosY - 1);
        }
        else if (key == Down)
        {
            gameState = move_player(world_map, playerPosX, playerPosY, playerPosX, playerPosY + 1);
        }
        else if (key == Quit)
        {
            gameState = QuitGame;
        }
        else
        {
            std::cout << "Invalid direction, use W,S,A,D to move up, down, left, right. Q to quit.\n";
        }

        print_map(world_map);
    }
}


C++ String Non-Member Operator Overloading

Style Advice

Do not try to do too much on one line. Constructs such as if ((a = lhs.find(rhs, s_loc)) != -1) ++out, s_loc = a + 1; are difficult to read, difficult to parse and prone to errors. Instead, let each control statement have its own block, and do not abuse the comma operator to do completely unrelated things. For example, the line I mentioned should rather be something like

if ((a = lhs.find(rhs, s_loc)) != -1) {
    ++out;
    s_loc = a + 1;
}

or, even better,

a = lhs.find(rhs, s_loc);
if (a != -1) {
    ++out;
    s_loc = a + 1;
}

Avoid magic numbers. In particular, when checking the result of string::find, check whether the result is equal to std::string::npos, not -1. There even are some places in the code where you are already doing this, so please adopt it for the remaining places as well.
Prefer early return on failure. Concretely, in operator- you return an empty string only after you have checked for and possibly returned a valid result. Although this is quite minor, I would have expected the method to work the other way around, i. e.

if (loc == std::string::npos) {
    return std::string();
}
return lhs.substr(0, loc) + lhs.substr(loc + rhs.size());

which is quite a common pattern.
if (rhs == "") return static_cast<unsigned long>(-1); works but is not expressive. Prefer

if (rhs.empty()) {
    return std::numeric_limits<unsigned long>::max();
}

while (rhs --> 0) out += lhs; does nothing but obscure what is going on. --> is not an operator in C++, so you should not write it as if it were one. Write while (rhs-- > 0) ...
Remove the typo from the comment to operator-; what currently reads "occrance" should probably be "occurance".

Possible Optimizations

Although generally your code appears to work well, there are some things that you could easily implement to improve performance, mostly to avoid unnecessary copies.

While it is good practice to implement operators in terms of other operators, the way that you implemented your operation + assignment-operators incurs making additional copies of your input. For example, let's consider operator% and operator%=:

inline std::string operator%(const std::string& lhs, const std::string& rhs) {
    //...
    auto out = lhs;
    //...
    return out;
}

inline std::string& operator%=(std::string& lhs, const std::string& rhs) {

    return lhs = operator%(lhs, rhs);
}

Since operator% does not modify its inputs, it has to make a copy which is then modified and returned. However, operator%= does modify one of its inputs, in this case lhs, so making a copy, modifying it and reassigning it to lhs is unnecessary. Instead, you could approach the problem the other way around: First, implement operator%=, then implement operator% in terms of it:

inline std::string& operator%=(std::string& lhs, const std::string& rhs) {
    //perform in-place operation on lhs
    return lhs;
}

inline std::string operator%(const std::string& lhs, const std::string& rhs) {
    std::string out = lhs;
    out %= rhs;
    return out;
}


Instead of giving your variables names like dd/mm/yyyy, you should give them names like day, month, year. Other names like d1 or d2. Should have better names.
You need some space between operators. For example, you have this condition in your code: if(dd<10). Stuff like this can be expanded to if(dd < 10). Variable definitions should also not look like this: var x=...;, but rather, var x = ...;.
You should add some more comments, e.g, describe what the functions/code blocks do, and how the processes behind them work.
Finally, just a small nitpicky thing. Both of your functions should be in camelCase. One of them is in underscore_case.


A positive to begin with - your code generally follows the style guide, which is an excellent start. Some docstrings would be nice, though (I have also omitted these, but they are generally a good idea). Also, your imports should really be on separate lines:

import random
import time

Having a global is generally a bad sign. Why not make your Player a class too? This would be very similar to Enemy, so we can do some inheritance:

class Character:

    def __init__(self, health):
        self.health = health

    def attack(self, other):
        raise NotImplementedError

Note that all Characters will have health and be able to attack, although at this point we don't supply an implementation for the latter. This separates the enemy_attack and attack_enemy methods into separate classes, which makes much more sense (the use of self is currently a bit confusing).

class Player(Character):

    def __init__(self, health=100):
        super().__init__(health)

    def attack(self, other):
        answer = input("What move would you like to make (punch, kick or headbutt)? ")
        if answer.lower() in ('punch', 'kick', 'headbutt'):
            other.health -= int(random.randint(1, 100) /
                                (random.uniform(0, 1) * other.defense))
        else:
            print("you stumble...")

The Player will only have health, and its attack is based on user input. Note the use of super to call the parent __init__, and the use of in to check the user's input.

class Enemy(Character):

    def __init__(self, name, strength, defense, health):
        super().__init__(health)
        self.name = name
        self.strength = strength
        self.defense = defense

    def attack(self, other):
        print("The {0.name} attacks...".format(self))
        other.health -= int(self.strength * random.uniform(0.1, 1.4))

The Enemy has some additional attributes, and the attack is entirely random. Note the use of str.format to provide output.

This isn't perfect, because:

a Player relies on other having a defense attribute, so two Players can't attack each other; and
health can fall below zero, which doesn't make much sense.

Perhaps you could refactor to solve these problems?

The battle logic is independent of which Characters are involved, so should become a separate function rather than an instance method:

def battle(player, enemy):
    print ("An enemy {0.name} appears...".format(enemy))
    # Combat loop
    while player.health > 0 and enemy.health > 0:
        player.attack(enemy)
        print("The health of the {0.name} is now {0.health}.".format(enemy))
        if enemy.health <= 0:
            break
        enemy.attack(player)
        print("Your health is now {0.health}.".format(player))
    # Display outcome
    if player.health > 0:
        print("You killed the {0.name}.".format(enemy))
    elif enemy.health > 0:
        print("The {0.name} killed you.".format(enemy))

Note that there is no need to break for the player.health, as the loop will end there automatically.

Now the overall game becomes:

if __name__ == '__main__':
    enemies = [Enemy("Boar", 10, 5, 100), Enemy("Wolf", 20, 10, 100),
               Enemy("Lion", 30, 20, 100), Enemy ("Dragon", 40, 30, 130)]
    battle(Player(), random.choice(enemies))

Note the use of if __name__ == '__main__', to allow e.g. import elsewhere without running the loop (so you can use the Characters in other scripts more easily), and the line break in enemies to keep line lengths reasonable.


A couple of quick comments:

You can use the see tag's cref attribute. If you generate documentation, some tools will generate hyperlinks for you.

/// <summary>
/// This wraps the .NET <see cref="StringBuilder"/> in a slightly easier to use format.
/// </summary>

The length property of a StringBuilder is read and writable. It's also really useful for it to be so:

var sb = new StringBuilder();
foreach (var i in Enumerable.Range(0,10))
{
    sb.AppendFormat("{0},", i);
}
sb.Length--; // removes the trailing comma.

That's a contrived example which is trivially served with string.Join but setting the length can be useful!

The _stringBuilder field should be readonly.

I'd say CurrentString is superflous. Just call _stringBuilder.ToString()

I must admit, personally I think the StringBuilder api is really good, I've never needed an implicit conversion to a string or felt the need to + rather than append to them.


How about using collections.Counter:

import string
from collections import Counter

def count_words(text):
    text = text.lower()
    for s in string.digits + string.punctuation:
        text = text.replace(s, "")
    text = text.split()
    return Counter(text)

print(count_words("Hello world, World War, War and Peace"))


Extract HTML tags and its content from a string

Using regex to parse valid html will work (and you might call it beautiful) until it doesn't work... then you'll bend over backwards (over and over) each time you encounter an anomaly then try to write a patch for the pattern.

Allow me to notify you of a simple, valid html occurrence that will break your sample pattern:

Demo

<link rel="stylesheet" href="/html_5/tags/html_link_tag_example.css">

It would match because your li needle is not immediately followed by a word boundary character (\b). Is this a simple thing to fix? Yes, but my point remains -- regex is an inappropriate tool for parsing valid html.

I generally rely on DomDocument for most cases and when XPath makes life simpler, I use that to perform clean, readable queries on the document.

This is one time when focussing on speed is a moot point -- speed is the least of your worries.  What good is speed if the results are bogus? The goal should be to design a robust and reliable script using DOM-aware techniques.


Your code has a few items in it which concern me in a few small ways, and, in general, while I think your code is OK functionally, it could be done better (I am biased, I have done similar things, and ended up doing it differently).

First up, my concerns...

Timestamp function

while it is a 1-liner, I would still have a method to encapsulate the time formatting:

func buildFileName() string {
    return time.Now().Format("20060102150405")
}

filepath

You should use path/filepath to manage your path names:

import (
  "path/filepath"
)

filenamePrefix := filepath.Join(exeDir, "debug", buildFileName())

Functions

You need more functions... still. I would create a function to control the file creation process (note the use of %d instead of string.Itoa...):

func pickFileName(prefix string) (string, error) {
    for i := 0; i < 100; i++ {
        fname := fmt.Sprintf("%s_%d", prefix, i)
        if _, err := os.Stat(fname); os.IsNotExist(err) {
            return fname, nil
        }
    }
    return "", fmt.Errorf("Unable to create a unique file with the prefix %v in 100 tries", prefix)
}

Result

OK, now your code looks like:

filenamePrefix := filepath.Join(exeDir, "debug", buildFileName())
filename, err := pickFileName(filenamePrefix)
if err != nil {
    log.Print(err)
    return
}

fd, err := os.Create(filename)
if err != nil {
    log.Print(err)
    return
}
defer fd.Close()

io.Copy(fd, request.Body)

Error handling

You have log.Print(err.Error()) but log.Print(err) is fine. Still, you should probably be returning an error, instead of just logging it....

Alternative

If it were me, I would do two things.....

First up, create a directory for the actual process you are running as.

myid := fmt.SPrintf("Process_%d", os.Getpid())

then, ensure you use that in your path:

filenamePrefix := filepath.Join(exeDir, "debug", myid, buildFileName())

Now, you have a folder dedicated to the current process (and collisions with other running instances of your process are impossible - at the same time - though there may be an old instance of that same folder for an older process - that's OK).

Then, create a counter (a "global") too:

var debugCounter uint64

And a matching function:

func nextDebugId() string {
    return fmt.Sprintf("%d", atomic.AddUint64(&debugCounter, 1))
}

Now, you can simplify the whole thing (by modifying buildFileName) to:

func buildFileName() string {
    return time.Now().Format("20060102150405") + "_" + nextDebugId()
}

and your whole code then becomes:

fullpath := filepath.Join(exeDir, "debug", myid, buildFileName())

fd, err := os.Create(fullpath)
.....


Counting number of 'if' statements in SAS

The code in question has some flaws which I will address here:

var index = new List<int>(); isn't really used and should be removed
var value = new List<String>(); isn't really used and should be removed
The method parameter List<String> list isn't used as a List<T> but rather like an array hence it should be an array.

Fixing these points leads to
private static int CountIfs(string[] linesOfCode)
{
    var count = 0;

    for (int i = 0; i < linesOfCode.Length; i++)
    {
        var line = linesOfCode[i];
        if (line.Contains("if"))
        {
            if (line.Contains("then") || line[i + 1].Contains("then"))
            {
                count++;
            }
        }
    }
    return count;
}

Now the code looks much cleaner and smaller.

An if will always be the first statement in a line of code after removing any leading whitespace and will have a trailing whitespace. Using Contains() can lead to many false positives. Assume a line of code contains the following text:
"I received the tif-file of this invoice from my partner in athen."

A then will always have a leading whitespace and either have a trailing whitespace or will be the first part of a line of code after removing leading whitespace.

Like mentioned in @Paparazzi's answer you could run into an IndexOutOfRangeException by accessing list[i + 1].

Let us introduce a new method which just handles the check for the if statement.
private static bool HasIfStatement(string line)
{
    return line.Trim().StartsWith("if ");
}

No we need a method to check for a then.
private static string[] spaceArray = new string[] { " " };
private static bool HasThenStatement(string line)
{
    return line.Split(spaceArray , StringSplitOptions.RemoveEmptyEntries).Any(w => w == "then");
}

Well this method is some kind of quick & dirty because it will create a lot of strings. I will leave it to you to make it better.
By adding a flag for the case that the line had an if and doesn't had an then we can't get an IndexOutOfRangeException anymore. As a sideeffect we could now use a foreach loop and have an IEnumerable<string> as the methods parameter type.
Putting all together could look like so
private static int CountIfs(IEnumerable<string> linesOfCode)
{
    var count = 0;
    var lastLineHadIf = false;
    foreach (var line in linesOfCode)
    {
        if (lastLineHadIf)
        {
            lastLineHadIf = false;
            if (HasThenStatement(line))
            {
                count++;
            }
        }
        else if (HasIfStatement(line))
        {
            if (HasThenStatement(line))
            {
                count++;
            }
            else
            {
                lastLineHadIf = true;
            }
        }
    }
    return count;
}

private static bool HasIfStatement(string line)
{
    return line.Trim().StartsWith("if ");
}

private static string[] spaceArray = new string[] { " " };
private static bool HasThenStatement(string line)
{
    return line.Split(spaceArray , StringSplitOptions.RemoveEmptyEntries).Any(w => w == "then");
}

I don't know about case-sensitivity of if and then so you may adjust the code to take this into account.
This code doesn't take comments into account, so you should adjust the code for comments as well.


I'm not a security expert, so my review will focus on general best practices first.

Code structure and style

1. Imports
You can group import that belong together, e.g

from Crypto.Hash import HMAC
from Crypto.Hash import SHA256

can be written as

from Crypto.Hash import HMAC, SHA256

The Official Style Guide for Python also recommends to place import from the standard library before any third party libraries. Apart from the double import of zlib you're already following this.

2. Documentation
You started to add some kind of documentation on decrypt, so encrypt should be treated the same. The officially recommended way is to provide function documentation surrounded by """...""" like so

def decrypt(key, ciphertext):
    """Initialize counter for decryption.

    iv should be the same as the output of encrypt().
    """
   #^--- you probably also mean ciphertext here

This will also allow Python's built-in help(...) function to pick up your documentation.

3. Whitespace
You should tidy up the newlines in your code. There are often two (sometimes more) newlines within a function's body, while there are none between the imports and functions. This might be an issue from posting the code here on Code Review, but you should definitely check that. A reasonable convention many people agree on is to use two newlines to seperate functions (or imports from the following functions) and only use a single newline in function bodys to visually create logical groups of code.

The code itself

1. Exceptions
"You're not doing the right thing!" What thing you might ask? Well, ... "Read it! Find it out yourself!"
Sounds frustrating, doesn't it? Python offers a wide variety of built-in exceptions so you can easily convey the type of error that has happened, without the need to parse/read the actual error message. In your case ValueError seems to be appropriate. From the documentation:

  exception ValueError
     Raised when an operation or function receives an argument that has the right type but an inappropriate value, [...]

In case of an error on checking the HMAC, you might add your own exception derived from Exception (maybe HMACValidationError?). I will leave this as an exercise to you.

2. encrypt/decrypt
These functions look quite reasonable from what I can jugde and work the way I would expect them to work. A minor "issue" I found, is that they are not handling string encoding/decoding symmetrically. While encrypt does handle unicode strings by converting them to bytes representation before encryption, decrypt has no way of knowing that this step has taken place and will always return the raw bytes.
At the moment, I would favor an approach where the application using those functions has to take care that the input is already encoded into bytes and also decode the bytes back into a unicode string if desired. This would also be in line with not handling string values for the key, which strongly hints that there is an earlier step where those are created and possibly encoded.

3. Security
From what I've seen on my journey through cryptoland, it is a quite common practice to simply prepend the ciphertext with the initialiation vector (IV). Password hashing functions do something similar and tend to store the salt prepended to the actual hash to have it available for validation later. Remember: the IV does not protect your ciphertext in the same way the key does, and therefore must not be handled with the same care. It instead prevents an attacker to see the same plaintext mapped the same ciphertext repeatedly, which is maybe more than you can afford to reveal.
Something similar applys to the HMAC. The security of the HMAC authentication should only rely on the secrecy of the key, and nothing else. So as long as you use a suitable HMAC implementation and a reasonably secure key, you should be good to go. Just bear in mind that an HMAC alone does not and cannot prevent all kind of attacks, e.g. HMAC alone cannot prevent replay.


That has been said before in previous reviews, there's not really a gain in having a Private Type that lets you define a Private this As tKeyValuePair, which forces you to use this. to access what would otherwise be private fields.

Actually there is one gain: doing that allows having a Key public member that encapsulates a Key private member, all without confusing poor old VB6 compiler.

Other than that, one has to see the List and possibly also the Dictionary implementation to see the benefits of the additional complexity introduced by IEquatable and IComparable interface implementations.

The code is fairly clean, straightforward and readable, breaking IEquatable and IComparable implementations into sub-functions respectively for reference and value types might be a little overboard, but it does make a nice abstraction level when one reads the implementation code.

One nitpick, the interface implementations and the corresponding sub-functions could declare a local result variable and assign the function's return value in only a single location:

Dim result As Boolean

If IsObject(kvp.Key) Then
    result = CompareReferenceTypes(this.Key, kvp.Key)
Else
    result = CompareValueTypes(this.Key, kvp.Key)
End If

IComparable_CompareTo = result

Dim result As Boolean

If IsObject(kvp.Key) Then
    result = EquateReferenceTypes(this.Key, kvp.Key)
Else
    result = EquateValueTypes(this.Key, kvp.Key)
End If

IEquatable_Equals = result

Dim result As Boolean
Dim equatable As IEquatable

If IsObject(this.Key) And TypeOf this.Key Is IEquatable Then
    Set equatable = value
    result = equatable.Equals(other)
Else
    Debug.Print "WARNING: Reference type doesn't implement IEquatable, using reference equality."
    result = (ObjPtr(value) = ObjPtr(other))
End If

EquateReferenceTypes = result

Looking more closely at the shape of the code, vertical whitespaces aren't all that consistent: there's more vertical whitespaces near the bottom of the code module, while the code near the top doesn't seem to follow the same spacing style. Minor nitpick, but for a small specialized class like this, it's an easy fix.


Golang WebSocket communication between server and app

You've reviewed your own code, and done a good job, actually:

//This feels weird
//I use a ticker that ticks with the fps of the camera
//to send the individual frames to the browser

ticker := time.NewTicker(time.Millisecond * 33)
for _ = range ticker.C {
        if err = conn.WriteMessage(websocket.TextMessage, s.Frame); err != nil {
                log.Fatal(err)
        }
}

Fundamentally the issue is that you've joined two separate problems in to one function, and the result is a poorly defined operation that's a bit brittle.

What you want to do is expose a channel that sends Frame instances each time one is available. This will result in code that looks more like:

for f = range s.Frames() {
        if err = conn.WriteMessage(websocket.TextMessage, f); err != nil {
                log.Fatal(err)
        }
}

Loop/range on a channel (assume s.Frames() returns a channel of Frame instances), and then send each Frame down the websocket.

This sets the channel as an "api" to the camera, and the logic is encapsulated in there.

Now, the challenge is to set up a source for that frame channel....

A separate goroutine that polls the camera for new frames, listens to a stream and breaks on each frame, or loops on a timer (like you have now), or whatever mechanism best suits managing your camera, would feed Frames in to the channel, and you've thus separated the two concerns.

You can also now have different mechanisms for sourcing frames, and still feed them in to the same method for delivering them to the websocket.


Getting location using LiveData and FusedLocationProviderClient

Let me explain how to work with LiveData - ViewModel and Activity.

Right now your BoundLocationManager looks good. I'd recommend not to make it's singleton. And also not consuming it directly in your UI (in this case WeatherFragment) code.

It's highly recommended to keep LiveData in ViewModel and your ViewModel will provide it the UI.

Also if you use LiveData, then never worry about removing the observer. It's done automatically for you.

Let me know if you still have some queries.


I have never used SQLite either, but something is jumping at me in your code: the addAuthor() procedure owns the connection, which means if you're calling it 10,000 times in a loop you're going to be opening and closing 10,000 connections, which is ..well to be honest, nuts.

Looking at the method's signature:

Sub addAuthor(ByVal authorlnf As String, ByVal authorfnf As String)

Naming nitpicks

I believe PascalCasing is the convention for naming just about everything in VB, but what really matters is consistency. Try to be consistent with the language you're using - the VB API uses PascalCasing for methods.
Names authorlnf and authorfnf could just as well be named authorwtf - they tell the reader it's something about an author, but to find out what exactly, one has to dig deeper than the method's signature. Avoid disemvoweling at all costs - it ruins reading even the best-written code base.
well now that I think of it I'd say authorlnf would be author's last name and authorfnf would be author's first name.. still scratching my head as for what the last f is standing for...

Serious stuff

What happens when authorlnf contains the value "Robert'); DROP TABLE author;--"?

credits: xkcd/Randall Munroe

Your insert fails and the author table gets dropped (if your user has the permissions and there's no FK constraint involved, but that's not the point). This is called SQL Injection and, depending on where, how and by whom your code is being used, it may or may not be a concern - nevertheless, it's a flaw in your design.

You could write yourself some parameter-cleansing function and think you're protected, but that would leave you the burden of proving that your cleansing function is undeniably safe. And you must never forget to call it!

Or you could use parameters, that take the value of the user input. That's much harder to mess with!

Connection ownership

I think the AddAuthor() method shouldn't create (/own) the connection it's using. Consider this:

Sub AddAuthor(ByVal connection As SQLiteConnection, ByVal authorlnf As String, ByVal authorfnf As String)

All of a sudden, opening, closing and disposing the connection is no longer a concern here: you put that burden on the caller, where you're looping 10,000 times - so you open up the connection, create and execute 10,000 commands inside AddAuthor(), and then close the connection. That puts the lite in SQLite, no?

I'll only add that you're catching known & relevant exceptions and letting unknown ones bubble up, and that's excellent. The only thing is, as @Malachi correctly pointed out, you should be closing the connection in a Finally block, but if your method doesn't own the connection, that's moot. However I believe a Command would implement IDisposable, so it should be .Dispose()'d.


Pinging the user requested host - is this code insecure?

Your regular expression for detecting valid urls is very weak and you can slip a lot past it.

Though you have used escapeshellcmd() to protect yourself a bit from people trying to run alternative commands you have not protected yourself from people sending extra parameters to ping.

Two alternative attacks spring to mind:

You can overload your servers by making them send lots of useless packets in the ping requests.
You can use your servers as the host for an attack on somebody else.

An example of sending extra bytes with ping (I put your script in the file test.php on myhost (not real name).

http://myhost.com/test.php?host=plop.com%20-s%20500

Generates:

ping -n 0 plop.com -s 500


Complexity in multiple if-else algorithms

Building on @alzaimar's answer:

You can unfold the conditionals with early return. The linear flow makes it slightly easier to follow.

if (listContainingLeft == null && listContainingRight == null)
{
    AddBranch(left, right);
    return;
}

if (listContainingLeft != null && listContainingRight != null)
{
    Combine(listContainingLeft, listContainingRight);
    return;
}

if (listContainingLeft != null)
{
    listContainingLeft.Add(right);
    return;
}

if (listContainingRight != null)
{
    listContainingRight.Add(left);
    return;
}


Stack class in Ruby to calculate maximum number, get the average, pop, push

Your code here is really just wrapping the Array class from the ruby library.  As such, your performance will be exactly the same as just using Array itself.  The array can hold as many items as you can hold in RAM and the performance will be determined by whatever machine you are running on.

The ruby array class is written in C and performs fairly well.  You ask about optimization but you don't given any criteria, so I can't really speculate beyond that.  If you need something that is extremely fast or needs to scale across millions or billions of items, you may want to look at a different language or even a database.

Looking at your code, you have several instances of the pattern if-elsif-else.  The last two are redundant and can be combined.  For example, the max of an array with one item will be the one item, you don't have to break it out.  Likewise for doing the average; you can average a list with one item and you will still get the correct answer

def max ()
    # returning 'puts' is _very_ unusual.  It would be better to return nil, which Array#max would do anyway
    return puts "Error no input" if @stacks.empty?
    @stacks.max     # the last expression is returned automatically
end

def average()
    return puts"Error no input" if @stacks.empty?

   sum = @stacks.inject(0, :+)
   sum / @stacks.size # note that if you sum an array of integers this way, the final average will also be an integer unless you convert something to a float first
end


Validating the presence of properties in a document

If your tests are about mandatory members in @opts you can do this:

MandatoryProperties = [:property_1, :property_2, ...]

def valid?
  @opts && MandatoryProperties.all? { |k| !@opts[k].nil? }
end


I ended up taking a bit of a different route with this problem.  It seems I was not adding additional proxied resources if they were not contained within the current compiled package... as in, they were included in included libraries.  So, my proxy utility looks like the following:

public abstract class GeneratedCodeRepository<T> {

    private ConcurrentMap<Class, T> generatedMap = new ConcurrentHashMap<Class, T>();

    public GeneratedCodeRepository(String repositoryPackage, String repositoryName) {
        loadRepository(getClass().getClassLoader(), repositoryPackage, repositoryName);
    }

    public T get(Class clazz){
        T result = generatedMap.get(clazz);
        if (result == null) {
            T value = findClass(clazz);
            if(value == null){
                return null;
            }
            result = generatedMap.putIfAbsent(clazz, value);
            if (result == null) {
                result = value;
            }
        }

        return result;
    }

    public abstract T findClass(Class clazz);

    /**
     * Update the repository class from the given classloader.  If the given repository class cannot be instantiated
     * then this method will throw a TransfuseRuntimeException.
     *
     * @throws TransfuseRuntimeException
     * @param classLoader
     */
    public final void loadRepository(ClassLoader classLoader, String repositoryPackage, String repositoryName){
        try{
            Class repositoryClass = classLoader.loadClass(repositoryPackage + "." + repositoryName);
            Repository<T> instance = (Repository<T>) repositoryClass.newInstance();
            generatedMap.putAll(instance.get());

        } catch (ClassNotFoundException e) {
            //nothing
        } catch (InstantiationException e) {
            throw new TransfuseRuntimeException("Unable to instantiate generated Repository", e);
        } catch (IllegalAccessException e) {
            throw new TransfuseRuntimeException("Unable to access generated Repository", e);
        }
    }
}

findClass() then can be defined to lookup the given repository class using reflection.


Code to get the IP address and format it to run in nmap

You can do it purely in Bash, using ${parameter%word} to strip off the last octet from the IP address.

hostname -I | while read ip _ ; do
    nmap -sn  ${ip%.*}.\*
done

Note that hostname -I is not portable: the -I option appears to be a GNU extension.


Compiling all Exception messages into a string

Readability first

  I've tried to condense everything to one line, but am unsure if this is the most efficient way of approaching this problem.

Your primary goal when writing code is readability and maintainability and not squeezing everything into single lines.

Can you read this?

return exception != null ? string.Format("{0}: {1}\n{2}", msgCount, exception.Message, GetExceptionMessages(exception.InnerException, ++msgCount)) : string.Empty;

I cannot. This line is way too long even on my large monitor.

How about this? A little bit better, but still, the third line is too long even with string interpolation instead of string.Format

return
    exception is null
        ? string.Empty
        : $"{messageCount}: {exception.Message}{Environment.NewLine}{GetExceptionMessages(exception.InnerException, ++messageCount)}";

Separation

We need to change the strategy because your method is doing two things at a time:

it's looking for exceptions and
it's building a string

We need to separate them by implementing an extension that will enumerate exceptions:

public static IEnumerable<Exception> DescendantsAndSelf(this Exception exception)
{
    do
    {
        yield return exception;
        exception = exception.InnerException;
    } while (!(exception is null));
}

Then we rewrite the string building method as an extension of the first one:

public static string StringifyMessages(this IEnumerable<Exception> exceptions)
{
    var messages = exceptions.Select((e, i) => $"{i + 1}: {e.Message}")
    return string.Join(Environment.NewLine, messages);
}

So, finally you can chain them like this:

catch (Exception e)
{
    Console.WriteLine(e.DescendantsAndSelf().StringifyMessages());
}

Advantages

This means more code now but this code is testable, modular and can be reused for other things and extensions.


Format capital letters at the start of a sentence

Multispace bug

There is one flaw in this solution. It assumes a perfectly formatted sentence. This means that if there are multiple spaces between sentences the formatting won't work. For example it turns this "foo.  bar!    baz?" into "Foo.  bar!    baz?"

To fix this you either need another extension fixing all multiple spaces with a single one or make this one aware of this problem.

if (source[index] == ' ')
{
  index++;
}

This if should contain a continue but even with it, it won't work yet because the first if won't pass the second time. The logic is more complex and it would require a flag to skip "" spaces.

Trim

result.Append(source.First().ToUpper());

You assume that the first char is the first letter but you don't trim the source in case it isn't.

Naming

if (IsPunctuationMark(source[i]))
{
    int index = i + 1;
    if (index >= source.Length || IsPunctuationMark(source[index]))
    {
        continue;
    }
    if (source[index] == ' ')
    {
        index++;
    }
    result.Append(' ');
    result.Append(source[index].ToUpper());
    i += index - i;
}

Here I find the index too general. It looks like it should be called e.g. firstLetterIndex.

Alternative? ;-)

You could use regex to clean-up the string.

public static string CapitalizeSentence(this string source)
{
    return
        Regex
            .Replace(
                source.Trim(),
                @"(^|(?<Punctuation>[.!?])\s*)(?<FirstLetter>[a-z])",
                m =>
                    $"{(m.Groups["Punctuation"].Success ? m.Groups["Punctuation"].Value + " " : string.Empty)}" +
                    $"{m.Groups["FirstLetter"].Value.ToUpper()}");
}


Testing the process of assigning offers to a customer

You are asking about your tests but there are issues in your code that should be addressed before you start testing.

public abstract class Product
{
    public abstract bool Check(Customer customer);
}

You made Product an abstract class but this is not necessary. It does not provide any default implementation for anything so an simple interface would be enough.

There is however one more issue with it. The Check method. It doesn't say anything about what is being checked.

customer._expenditure >  100 && customer._gender =="M"

Its implementation evaluates the above condition but there is no information about it in the method name so nobody could tell what it checks.

On top of it it even uses the hardcoded magic-number 100. This should be a constant.

You also put it inside an if/else which isn't necessary. You can just return the result.

public void AssignOffers(IOfferCalculator offerCalculator, IList<Product> products)
{
    foreach (var product in offerCalculator.Calculate(this, products))
    {
        if (!_assignedProducts.Contains(product))
        {
            _assignedProducts.Add(product);
        }
    }
}

This is the method you test but it's difficult to test it because it's doing to much: it filters the product list, it checks whether it's assigned and it adds it to another list. To me these are three different responsibilities. You should at least pass an already filtered product list.

The method also does not inform me that anything will be filtered at all. It says AssignOffers but instead of assing offers it calculates something and it works with producs that it adds to another list but it does not assign anything. Everything is misleading about this method and it's full of surprises.

Having said that I find all your tests are meaningless because the implementation is meaningless too. The code is confusing and doing completely different things that the names would suggest. Writing tests for such code is pointless.

You should first fix the main code so that it's clear what it does so that you know what you test. Try to pick names that clearly communicate the purpose and do exacly only that.

For exmaple if you Calculate something then let this method return a result of this calculation based on as few parameters as possible. Passing entire objects where you only need two properties is a code smell.

public IEnumerable<Product> Calculate(Customer customer, IList<Product> products)
{
    foreach (var product in products)
    {
        if (product.Check(customer))
        {
            yield return product;
        }
    }
}

But wait a minute! Let's take a look at what Calculate actually does... to my (not) surprise it does not calculate anything but instead it checks products against a customer!!! How confusing is that?

This is such a mess that you can throw all these tests away and first rewrite the code completely then write new tests.


Retrieve recognised text from JSON returned by Microsoft OCR

One way to almost halve the number of lines (and levels of indentation) needed is to use dict.get with [] as the optional default option:

def check_for_word(ocr):
    for region in ocr["regions"]:
        for lines in region.get("lines", []):
            for word in lines.get("words", []):
                if "text" in word:
                    return word["text"].lower()
    else:
        raise KeyError("OCR word not found")

I would also move the printing outside the function, so you can immediately return and add a else clause to catch the case that it is not present (this part could also be done outside with your code by checking for None).


Handling selections for a vending machine

The biggest thing you can do is to use string formatting to remove redundancy. Now instead of having to repeatedly type "1 for Coke 2 for Tango Cherry 3 for Dr Pepper:, you factor out the common parts, put the foods in a list, and use that list to populate the string. I also removed your first assignment to item, (ie item = "Gum") for each, because they immediately get removed. Finally, all three circumstances can be combined by indexing into a list of lists.

format_string = "1 for {0} 2 for {1} 3 for {2}:"

items = [["Diary Milk", "Maltesars", "Kit kat"],
         ["Airwaves","Extra Pepermint","Wriggley's Summer fruit"],
         ["Coke", "Tango Cherry", "Dr Pepper"]]

print(format_string.format(*items[selection - 1]))
new_selection = int(input("Please enter your option:"))
item = items[selection - 1][new_selection - 1]
itemValue += 1

Taken together, this reduces your code from 50 lines, to 10.


Applying dynamic filters to a SQL query using Python conditional expressions

The code in the post seems fine. I think it would be easier to read if you kept the lines to 79 columns or less, put each conditional in its own statement, and made the field names match the query parameter names, like this:

query = """
    SELECT AVG(stock_date - order_date) AS c, product_category
    FROM data_table_data
    WHERE stock_date BETWEEN :stock_date_start AND :stock_date_end
"""
if category != 'All Category':
    query += "AND product_category = :product_category "
if store_location != 'All Stores':
    query += "AND product_location = :product_location "
query += "GROUP BY product_category"
params = dict(stock_date_start=startDate,
              stock_date_end=endDate,
              product_category=category,
              product_location=store_location)
data = engine.execute(text(query), **params).fetchall()

Alternatively, you could push the conditional down into the SQL, and then the query would be constant:

QUERY = """
    SELECT AVG(stock_date - order_date) AS c, product_category
    FROM data_table_data
    WHERE stock_date BETWEEN :s1 AND :s2
    AND product_category IN ('All Category', :product_category)
    AND product_location IN ('All Stores', :product_location)
    GROUP BY product_category
"""


An idiom to use the same view function to create or edit an object?

I have looked at your code and I think that it actually looks pretty clean and straight forward. However, I would suggest that you make it more DRY by using reverse() to figure out what action to assign to the form.

views.py:

if task_id:
    action = reverse(edit, args=[task_id])
else:
    action = reverse(edit)
d['action'] = action

edit.html:

<form action="{{ action }}" method="post">
    {{ form.as_p }}
    <input type="submit" value="Save!" />
</form>


Best practice would be to separate the pure operations of your program from those that actually require IO. Counting the frequency of elements in a list doesn't require any IO, so you should tease that fromFile function apart into its constituent components for reusability, testing, comprehensibility, or whatever other purpose you'd like.

frequencies :: Ord k => [k] -> Map k Int
frequencies = fromListWith (+) . (`zip` [1,1..])

fromFile :: FilePath -> IO (Map Char Int)
fromFile = fmap frequencies . fmap unpack . readFile

I'd tweak this just a bit further, using repeat from the Prelude to build the infinite list instead of abusing list ranges, and to take advantage of the Functor laws and drop a few characters. I flip back and forth on writing functions in pointfree style when it requires infix sectioning too, in this case I'd probably keep the points but I don't know that one choice is clearly better than the other.

frequencies ks = fromListWith (+) $ zip ks (repeat 1)

fromFile = fmap (frequencies . unpack) . readFile


n-dimensional Euclidean space calculation templates

To reduce redundancy, I'd probably move the check for the inputs being the same size into a function template by itself, and just invoke that from the other three:

template <typename T>
void check_size(std::vector<T> const &a, std::vector<T> const &b) {
    if (a.size() != b.size())
        throw std::domain_error("vector addition must have equal length vectors");
}

I'd then at least test with passing one of the input parameters by value, using it as the destination for the calculation, and returning it:

template <typename T>
std::vector<T> operator+(std::vector<T> a, const std::vector<T>& b)
{
    check_size(a, b);
    std::transform(a.begin(), a.end(),
                   b.begin(),
                   a.begin(),
                   std::plus<T>());
    return a;
}

Although this doesn't always improve speed, it does often enough to be worth testing/profiling to see how well it works in this case.

Another possibility to consider would be using std::valarray instead. It's the forgotten step-child (so to speak) of the C++ standard, but it was designed specifically to support fast numeric calculations. It already defines equivalents of your + and - operators, as well as a sum and * operators, so your code comes out something like this:

std::valarray<double> origin{ 0, 0, 0 }, a{ 3, 4, 5 }, b{ -1, -2, -3 }, g{ 0, 0, 0, 0 };
say(origin);
say(a);
say(b);
say(a + b);
say(a - b);
say(b - a);
say(((a - origin) * (a - origin)).sum());
say(((b - origin) * (b - origin)).sum());

Since std::valarray defines all the operators we're using, the only other code we need is the say macro and the operator<< overload (minutely modified to take a std::valarray parameter instead of a std::vector).

Considering your more specific questions:

specific questions:

  can the squared_distance calculation be made more efficient?

std::valarray and/or std::array might help--but for a relatively small number of dimensions, I wouldn't expect to see much difference.

  is there a nice way to reduce code duplication in the + and - operators?

The code above attempts to answer this, to at least some degree.

  I've tested with int, float, double and std::complex. What else might be useful?
  is there any point to accommodating unsigned types?

I don't see a lot of point in unsigned types for this task. Yes, distances are always positive, but you can pretty easily end up with a negative number from an intermediate calculation, and you don't want that wrapping around to a large number as it would with an unsigned type.

  should I do anything about possible numeric overflow or underflow?

Harder to say. It basically depends on the use to which you're putting the code. If you need the distance, there are ways of computing it that avoid the larger magnitude of the squared distance (even as an intermediate), and are fairly fast to compute as well--but they're still slower than computing the squared distance, in which case the final result is also often the single largest value you deal with, so there's not a lot you can about overflow.

In floating point, subtraction is one of the prime culprits that can/will lead to precision loss. At least in theory, you might prefer to avoid it if possible, but for finding a squared distance I don't know of a lot of alternatives either.

  is there any point to having an #ifndef header guard in this file?

The compiler will (or should, anyway) give errors if you include it twice in the same translation unit, so it wouldn't hurt. Of course, you're unlikely to include it twice in the same translation unit directly, but its getting included via two other headers wouldn't be all that rare an occurrence.

I was going to mention std::array as well, but while I was writing this, I see @Morwenn has written one about that already.


Custom log4net raising application-level events

I log events to a file/database and have a need to also raise those events to the application itself (for display purposes).

Generally this is not a bad idea but...

  I the case of my requirements, this event may be called 30 times/sec across multiple different threads.

with this many log events per second it is. No human can read it anymore unless you are writing applications for Jedis.

Use a custom named logger and send to the UI only those messages that have some value to the user not just everything that gets logged.

Or create some kind of a filter that reduces the number of messages to a reasonable count.