Replace a^b by Pow(a,b) The application uses NCalc to execute diverse calculations.

The 'natural' way to write powers using a computer is using the ^ symbol. However, NCalc already uses it as bitwise or.

I use the following function to parse calculations and convert it to the Pow(a,b) format.

    Private Function _replacePower(Str As String) As String
        Const PowerString As String = "Pow({0},{1})"

        Dim i, j As Integer
        Dim c As String
        Dim before, after, all As String

        Dim other_p As Integer 'Keep track of other opening/closing parenthesis, to avoid breaking for a nested one.

        other_p = -1
        i = 1
        While i <= Len(Str)
            c = Mid(Str, i, 1)
            If c = "^" Then
                If Mid(Str, i - 1, 1) = ")" Then
                    j = i - 1
                    Do While Mid(Str, j, 1) <> "(" Or other_p > 0
                        If Mid(Str, j, 1) = ")" Then other_p = other_p + 1
                        If Mid(Str, j, 1) = "(" Then other_p = other_p - 1
                        j = j - 1
                    Loop
                    before = Mid(Str, j, i - j)
                Else
                    j = i - 1
                    'The expression to be raised is everything between the power and + - * / , ( <- Opening parenthesis is not ok if there is no closing one, and this case is handled above.
                    Do While (Mid(Str, j, 1) <> "+" And Mid(Str, j, 1) <> "-" And Mid(Str, j, 1) <> "*" And Mid(Str, j, 1) <> "/" And Mid(Str, j, 1) <> "," And Mid(Str, j, 1) <> "(")
                        j = j - 1
                        If j = 0 Then Exit Do
                    Loop
                    before = Mid(Str, j + 1, i - j - 1)
                End If

                other_p = -1

                If Mid(Str, i + 1, 1) = "(" Or other_p > 0 Then
                    j = i + 1
                    Do While Mid(Str, j, 1) <> ")"
                        If Mid(Str, j, 1) = ")" Then other_p = other_p - 1
                        If Mid(Str, j, 1) = "(" Then other_p = other_p + 1
                        j = j + 1
                    Loop
                    after = Mid(Str, i + 1, j - i)
                Else
                    j = i + 1
                    Do While (Mid(Str, j, 1) <> "+" And Mid(Str, j, 1) <> "-" And Mid(Str, j, 1) <> "*" And Mid(Str, j, 1) <> "/" And Mid(Str, j, 1) <> "," And Mid(Str, j, 1) <> ")")
                        j = j + 1
                        If j = Len(Str) + 1 Then Exit Do
                    Loop
                    after = Mid(Str, i + 1, j - i - 1)
                End If

                all = before & "^" & after
                Str = Replace(Str, all, String.Format(PowerString, before, after))
                i = 1
            End If
            i = i + 1
        End While
        Return Str
    End Function

My issues lies with performance, as it appears that this function is the bottleneck of the entire application. How could I improve?

One of your biggest performance issues is, using Mid to get one character from the string.  This gets the character as a string which is inefficient.  It would be better to get the character as a Char, by using the index operator of the String class(Str(j)).  To compare with literal characters use the character indicator(c)at the end of the string quotes("+"c).

Whenever you have code duplication try to find a way to break that code out into a separate function.  A case in point is when you are checking for operators in your string a simple function:

Private Function isOperator(chr As Char) As Boolean
    Return chr = "+"c OrElse chr = "-"c OrElse chr = "*"c OrElse chr = "/"c OrElse chr = ","c OrElse chr = ")"c
End Function

simplifies and unclutters your main code.

Whenever you have chained conditionals as above, use the shortcut boolean operators(OrElse and AndAlso).  This way a false result will return right away instead of reading the rest.

Don't use Len() to get the length of the string.  The String class has a length property that you can use and put into a variable.

Without any concrete examples of the complexity of the formulas that you're expecting, it's difficult to critique the efficiency of your approach.  I'm thinking though, that as a general rule, anytime you are doing extensive string manipulations you should definitely look at using a StringBuilder.

Simplified with these recommendations your code would look something like this:

Private Function _replacePower(Str As String) As String
    Const PowerString As String = "Pow({0},{1})"

    Dim i, j As Integer
    Dim c As Char
    Dim before, after, all As String
    Dim length As Integer = Str.Length
    Dim other_p As Integer 'Keep track of other opening/closing parenthesis, to avoid breaking for a nested one.

    other_p = -1
    i = 1
    While i <= length
        c = Str(i)
        If c = "^"c Then
            If Str(i - 1) = ")"c Then
                j = i - 1
                Do While Str(j) <> "("c Or other_p > 0
                    If Str(j) = ")"c Then other_p = other_p + 1
                    If Str(j) = "("c Then other_p = other_p - 1
                    j = j - 1
                Loop
                before = Mid(Str, j, i - j)
            Else
                j = i - 1
                'The expression to be raised is everything between the power and + - * / , ( <- Opening parenthesis is not ok if there is no closing one, and this case is handled above.
                Do While (Not isOperator(Str(j)))
                    j = j - 1
                    If j = 0 Then Exit Do
                Loop
                before = Mid(Str, j + 1, i - j - 1)
            End If

            other_p = -1

            If Str(i + 1) = "("c Or other_p > 0 Then
                j = i + 1
                Do While Str(j) <> ")"c
                    If Str(j) = ")"c Then other_p = other_p - 1
                    If Str(j) = "("c Then other_p = other_p + 1
                    j = j + 1
                Loop
                after = Mid(Str, i + 1, j - i)
            Else
                j = i + 1
                Do While (Not isOperator(Str(j)))
                    j = j + 1
                    If j = length + 1 Then Exit Do
                Loop
                after = Mid(Str, i + 1, j - i - 1)
            End If

            all = before & "^" & after
            Str = Replace(Str, all, String.Format(PowerString, before, after))
            i = 1
        End If
        i = i + 1
    End While
    Return Str
End Function

Private Function isOperator(chr As Char) As Boolean
    Return chr = "+"c OrElse chr = "-"c OrElse chr = "*"c OrElse chr = "/"c OrElse chr = ","c OrElse chr = ")"c
End Function

I took a more in depth look at your code, and realized there were quite a few more improvements that could be made.

Don't be afraid to be verbose with your variable names.  If you're worried about extra typing, Intellisense will bring up suggestions as you type, allowing you to just tab over the suggestion you want, instead of typing it all out.

You seem to do a lot of checking for different types of characters. Really all you need to check for is digits and brackets.

When checking for matching brackets, it is useful to have a `Dictionary(Of, Char, Integer)'.  Opens are +1, closings are -1.

The Char class has an IsDigit function that simplifies checking for those.

There is another large code duplication when you check to the left and the right of the caret to get the appropriate strings.

You didn't mention whether there is a possibility of more than one caret symbol in a string.  It seems logical that this could be a possibility.  My though is make the function recursive to keep checking the returned string for caret's.

Putting this all together, you might come up with something like this:

Private Function FixString(value As String) As String
    Dim caretIndex = 0
    caretIndex = value.IndexOf("^"c)
    If caretIndex <> -1 Then
        Dim sb As New StringBuilder()
        Dim leftHand = ""
        Dim rightHand = ""
        Dim leftHandIndex = 0
        Dim rightHandIndex = 0
        leftHandIndex = caretIndex - 1
        rightHandIndex = caretIndex + 1
        leftHand = getString(value, caretIndex, leftHandIndex, -1)
        rightHand = getString(value, caretIndex, rightHandIndex, 1)
        sb.AppendFormat("{0}Pow({1},{2}){3}", value.Substring(0, leftHandIndex + 1), leftHand, rightHand, value.Substring(rightHandIndex))
        Return FixString(sb.ToString)
    Else
        Return value
    End If
End Function

Private Function getString(value As String, caretIndex As Integer, ByRef offset As Integer, increment As Integer) As String
    Dim bracketTotal = 0
    Dim index = offset
    If Char.IsDigit(value(index)) Then
        While index <> value.Length AndAlso Char.IsDigit(value(index))
            index += increment
        End While
    ElseIf brackets.ContainsKey(value(index)) Then
        bracketTotal += BRACKETS(value(index))
        index += increment
        While index <> value.Length AndAlso bracketTotal <> 0
            If BRACKETS.ContainsKey(value(index)) Then
                bracketTotal += BRACKETS(value(index))
            End If
            index += increment
        End While
    End If
    offset = index
    If increment = -1 Then
        Return value.Substring(index + 1, caretIndex - index - 1)
    End If
    Return value.Substring(caretIndex + 1, index - caretIndex - 1)
End Function

Private ReadOnly BRACKETS As Dictionary(Of Char, Integer) = New Dictionary(Of Char, Integer)() From
{
     {")"c, -1},
     {"("c, 1}
}

I didn't see anything in your code, that would suggest the possibility of badly formatted strings, so I assumed that validation was being handled elsewhere.


Mixing controlled/uncontrolled behaviour with `getDerivedStateFromProps` I have a component that displays a bunch of selectable options in a single row. It takes a pageSize prop from the parent component due to responsiveness requirements.
If the pageSize prop ever changes, I need to reset the currentPage (currently active page), which is held in state, to 0.
class OptionsRow extends React.PureComponent {
    state = {
        currentPage: 0
    }
    ...
    static getDerivedStateFromProps(props, state) {
        // Reset page on pageSize change
        return {
            currentPage: props.pageSize === state.prevPageSize
                ? state.currentPage
                : 0,
            prevPageSize: props.pageSize
        }
    }
    ...
}

This code works as intended.
The reason I'm asking for a review is that React in it's documentation warns against getDerivedStateFromProps so I'm not sure if this is a correct usage. In order to avoid derived state, it recommends making the component completely controlled/uncontrolled, so I should lift the currentPage up to the parent component. But is this really necessary? Is something wrong with my solution except for "verbose code"?
Thank you!

There is a small and subtle issue with your usage of getDerivedStateFromProps.

getDerivedStateFromProps is invoked right before calling the render
method, both on the initial mount and on subsequent updates. It should
return an object to update the state, or null to update nothing.

and

Note that this method is fired on every render, regardless of the cause.

Your implementation returns a new state object every render regardless, but it should return null when nothing needs to be updated. By returning a new state object each render then unnecessary re-renders may occur in children components if passed any values from this component's state as they would fail shallow object comparison.
class OptionsRow extends React.PureComponent {
  state = {
    currentPage: 0
  }

  ...

  static getDerivedStateFromProps(props, state) {
    if (props.pageSize !== state.prevPageSize) {
      // Reset page on pageSize change
      return {
        currentPage: 0,
        prevPageSize: props.pageSize
      }
    }

    // Nothing changed, return null to keep existing state
    return null;
  }

  ...

}

However, in 3+ years working with react I've not ever had any compelling reason to reach for getDerivedStateFromProps and the docs even specify

This method exists for rare use cases where the state depends on
changes in props over time.

The last bit bolded is the important bit. Your component's behavior appears to simply be an effect of a prop updating, i.e. a side-effect to reset state, and could be implemented via the componentDidUpdate lifecycle method.
Solution 1 - Bronze
class OptionsRow extends React.PureComponent {
  state = {
    currentPage: 0,
  }

  ...

  componentDidUpdate(prevProps, prevState) {
    // Reset page on pageSize change
    if (this.props.pageSize !== prevState.prevPageSize) {
      this.setState({
        currentPage: 0,
        prevPageSize: this.props.pageSize,
      });
    }
  }

  ...

}

But notice now that the state currentPage only needs to be reset when props.pageSize updates, so you can directly compare current and previous props and drop the storage of the "last" pageSize in local component state.
Solution 2 - Silver
class OptionsRow extends React.PureComponent {
  state = {
    currentPage: 0,
  }

  ...

  componentDidUpdate(prevProps) {
    // Reset page on pageSize change
    if (this.props.pageSize !== prevProps.pageSize) {
      this.setState({ currentPage: 0 });
    }
  }

  ...

}

There is still room for improvement though, using the third bullet point from the docs.

If you want to “reset” some state when a prop changes, consider either
making a component fully controlled or fully uncontrolled with a key
instead.

What this means is you can completely forego the getDerivedStateFromProps and componentDidUpdate logic to check and compare the props' pageSize value and simply use a react key that updates when the pageSize does.

When a key changes, React will create a new component instance rather
than update the current one.

Solution 3 - Gold
<OptionsRow key={pageSize} pageSize={pageSize} />


Arrow anti pattern with crutch How can I improve this code and avoid crutch? Can I use a case operator?

public bool Crutch;

public void Resize(SizeChangedEventArgs input)
    {
        if (input.NewSize.Width < 450 && Crutch)
        {
            if (!ServiceLocator.Current.GetInstance<MultiViewModel>().EightBoxesCommandIsEnabled)
            {
                ChangeToSmall(2, 2);
            }
        }

        if (input.NewSize.Width < 550 && !Crutch)
        {
            if (!ServiceLocator.Current.GetInstance<MultiViewModel>().EightBoxesCommandIsEnabled)
            {
                ChangeToSmall(3, 2);
            }
            Crutch = true;
        }

        if (input.NewSize.Width > 450 && Crutch)
        {
            if (!ServiceLocator.Current.GetInstance<MultiViewModel>().EightBoxesCommandIsEnabled)
            {
                ChangeToBig(3, 2);
            }
            Crutch = false;
        }

        if (input.NewSize.Width > 550 && !Crutch)
        {
            if (!ServiceLocator.Current.GetInstance<MultiViewModel>().EightBoxesCommandIsEnabled)
            {
                ChangeToBig(4, 2);
            }
        }
    }

If ServiceLocator.Current.GetInstance<MultiViewModel>().EightBoxesCommandIsEnabled evaluates to true you never do anything; so hoist it out of the if statements and do an early return:

public void Resize(SizeChangedEventArgs input)

    if(ServiceLocator.Current.GetInstance<MultiViewModel>().EightBoxesCommandIsEnabled)
        return;

    if (input.NewSize.Width < 450 && Crutch)
    {
        ChangeToSmall(2, 2);
    }
    //...
}

You can name crutch into something better that describes what it represents. Or keep whether ChangeToSmall or ChangeToBig was called last and replace crutch with that property.

void ChangeToBig(int, int){
    lastChangeCalledBig = true;

    //...
}

void ChangeToSmall(int, int){
    lastChangeCalledBig = false;

    //...
}


Performance of hashmap-based session object Unfortunately, I can't use the Tomcat session for storing the key/value pairs for each user (restricted because it's an IVR domain-based project).

But I need the same functionality like a Tomcat session.

For every user I can get a unique id (call Id). So it's like the Tomcat sessionId.

I have developed the following class to achieve the Tomcat session functionality with my little knowledge of Java.

/** enum is used for Singleton instance  **/
public enum SessionObject {

    /** single instance  **/
    INSTANCE;

    /** ConcurrentHashMap to avoid external synchronization  **/
    /** Key as callId , value as HashMap to store the key value pairs  **/
    private ConcurrentHashMap<String , ConcurrentHashMap<String,String>> session =  new  ConcurrentHashMap<String , ConcurrentHashMap<String,String>>();

    /** Used to create a new Map when new call ivr is established in the first vxml file**/
    public void createSession(String callId){

        ConcurrentHashMap<String,String> callMap = new ConcurrentHashMap<String,String>();
        session.putIfAbsent(callId, callMap);
    }

    /** Used to get the existing stored values for the particular call Id **/
    public ConcurrentHashMap<String,String> getSession(String callId){

        return session.get(callId);
    }

    /** Used to remove the details of the particular call Id , when call is disconnected **/
    public void deleteSession(String callId){

        session.remove(callId);
    }

}

I think that the code above will be more fine tunable. Suggestions to improve the performance would be appreciated.

This is pretty simple code, with not a lot of room for improvement.

You can make the session variable final, and it's good to do so:

private final ConcurrentHashMap<String, ConcurrentHashMap<String, String>> session = new ConcurrentHashMap<String, ConcurrentHashMap<String, String>>();

You can simplify createSession a bit by dropping the pointless local variable:

public void createSession(String callId) {
    session.putIfAbsent(callId, new ConcurrentHashMap<String,String>());
}

Some of the comments are really pointless, for example these:

/** enum is used for Singleton instance  **/
public enum SessionObject {

    /** single instance  **/
    INSTANCE;

You're using spaces in a non-standard way. Instead of this:

private ConcurrentHashMap<String , ConcurrentHashMap<String,String>> session =  new  ConcurrentHashMap<String , ConcurrentHashMap<String,String>>();

Don't put a space in front of commas, but put one after, like this:

private ConcurrentHashMap<String, ConcurrentHashMap<String, String>> session = new ConcurrentHashMap<String, ConcurrentHashMap<String, String>>();

It's good that you comment your methods. But instead of this:

/** Used to create a new Map when new call ivr is established in the first vxml file**/
public void createSession(String callId){

The correct format for JavaDoc comments is like this:

/**
 * Used to create a new Map when new call ivr is established in the first vxml file
 *
 * @param callId the call id for which to create new session
 */
public void createSession(String callId) {

Discussion

Our discussion in comments is perhaps worth including in the answer itself.

  Is it necessary to make the method argument as final ?

In your code, it's not necessary. But it's recommended. By setting method parameters final, you can prevent reassigning the value by accident. It's a bad practice to reassign the values of method parameters, for example in here: void fun(int something) { something = 3; }

  Can you please tell me, why you put the session variable as final ?

It's a good practice to make member fields final, as much as possible. final variables cannot change, they are immutable. Being immutable makes a variable more reliable and robust: once created correctly, you cannot break it, for example by accidentally reassigning to a value that wasn't correctly validated.

  I have used the enum for singleton . So my class is thread safe. Also I declared a session variable as private . So, Is it necessary to use the concurrent hashmap instead of ordinary hashmap in this case ?

The enum singleton guarantees that all threads will see the same INSTANCE. However, if you have multiple threads, they might access the session map concurrently, which would not work with a regular hashmap. So yes, session should be a ConcurrentHashMap, unless the rest of your implementation guarantees single-threaded access.

  By adding the final, the session variable become immutable. But I am adding the key/value pairs in the 'cmap'. So the map will become mutable.

I meant in the more general sense. For example simple types like int would be truly immutable. With object types, only their reference, the variable pointing to them will be immutable. Although their content is still editable (why you mustn't forget), not being able to reassign them does give you some level of protection. So it's always good to make your members final whenever possible. When your solution requires some members to be not final, give it some thought to refactor your solution to make them final. This is not always possible, but you should at least try, always.


AngularJs and Google Bot experiment I have learned the question of solving Angular app optimization for search engines, and was frustrated that the most recommended option is prerendering HTML.

After some time spent, I suggested to create a directive that will load the bot after rendering templates or update scope.

Directive:

angular.module('test', []).directive('seoBot', ['$timeout', function($timeout){

    return {

        link: function($scope, element, attrs) {

            //publish event
            $scope.$on('runbot', function(){

                //hint for start after render
                $timeout(function () {

                    //find previos google's bot
                    var googleBot = document.getElementById('googleBot');

                    //if found - remove
                    if(googleBot) googleBot.parentNode.removeChild(googleBot);

                    //this is standard code from GA, but with ID "googleBot"
                    (function(i,s,o,g,r,a,m){
                        i['GoogleAnalyticsObject']=r;
                        i[r]=i[r]||function(){
                            (i[r].q=i[r].q||[]).push(arguments)
                        },i[r].l=1*new Date();
                        a=s.createElement(o),
                        m=s.getElementsByTagName(o)[0];
                        a.id="googleBot";
                        a.async=1;
                        a.src=g;
                        m.parentNode.insertBefore(a,m);
                    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

                    ga('create', 'UA-46685745-1', 'rktdj.com');
                    ga('send', 'pageview');

                    //log that bot is loaded
                    console.log("seo-bot loaded");

                    //check page to be rendered completely
                    console.log(document.body.innerHTML);

                }, 0, false);

            });

        }

    };

}]);

In controller:

//get some data from server and update scope
$http.get(GLOBAL.api.SERVER_PATH + GLOBAL.api.facultiesByCourse + s_id)
        .success(function (data) {

            $scope.courses = data;
            console.dir($scope.courses);

            //broadcast event to seo-bot
            $scope.$broadcast('runbot');

        } );

seo-bot tag in the bottom of template:

It works, and the bot loads every time when template have update or route changed.
I can check that all elements with data was rendered before.

But... will this solution really work? Will Google bot correctly correlate rendered page with the domain? Can I be banned for this?

As you found yourself, this has to work.

I only have 2 minor nitpickings:

Do not use console.log() in production code..
You are not using element and attrs, you might as well declare function($scope)

Very nice code.


Print Bitcoin price by getting JSON data using CoinDesk API In this code in Python, I used an API called CoinDesk to get the Bitcoin price as JSON.
I'm trying to follow the best practices in coding, so, I've used logging and I'm using a code formatter in my editor.
Kindly, any enhancements on this code will be appreciated.

# Project Idea: Bitcoin price program using CoinDesk API with Python.

# Importing libraries
import requests as rq
from datetime import datetime
import logging
import objectpath  # Read the library doc: http://objectpath.org/

def get_api_data():

    LOG_FORMAT = '%(levelname)s : %(asctime)s - %(message)s'
    logging.basicConfig(level=logging.DEBUG,
                        format=LOG_FORMAT, filemode='w')  # , datefmt='%d-%b-%y %H:%M:%S'
    logger = logging.getLogger()

    try:
        API_LINK = 'https://api.coindesk.com/v1/bpi/currentprice.json'   # CoinDesk API Link
        API_Data = rq.get(API_LINK)
        logger.debug("Getting API data - Done")

        api_data = API_Data.json()
        logger.debug("Converting API data to JSON - Done")
        return api_data

    except Exception as e:
        print("Exception occurred: ", e)
        logger.debug("Exception occurred.")

def main():

    # Getting the data from API
    data = get_api_data()
    tree_data = objectpath.Tree(data)

    time = tuple(tree_data.execute('$..updated'))

    usd = list((tree_data.execute('$..USD')))
    price = dict(usd[0])
    price_usd = price["rate"]
    price_description = price["description"] + ' - USD'

    print(f"The price in {price_description}: {price_usd}")
    print(f"Time of the price: {time}")

if __name__ == '__main__':
    main()

In general you should be more especific on the management of the exceptions, my only contribution will be to be more specific on them on the requests component. For example your code on:

def get_api_data(url):
    try:
        response = rq.get(url)
        response.raise_for_status()
        ...
    except Exception as e:
        logger.debug("Exception occurred.", exc_info=True)

With more specific exception management

def get_api_data(url):
    try:
        response = rq.get(url)
        response.raise_for_status()
        ...
    except requests.exceptions.RequestException as _:
        logger.debug("Exception occurred on request.", exc_info=True)
    except json.decoder.JSONDecodeError as _:
        logger.debug("Exception occurred on json decoder.", exc_info=True)


Copying a Dictionary and Replace its Values I have a custom dictionary where the values are numpy arrays. I would like to have a convenience function that creates a copy of its content except that the values are changed. If no value is passed, then the default None is assigned to the keys, else a value is set. The function can also work with a callable function, and then the resulting value is obtained by passing the length of the numpy arrays.

Inspired by what I found here, this is what I have so far:

import sys
import numpy as np

class CustomDict(dict):

    def strip(self, value=None):

        if callable(value):
            tmp = dict.fromkeys(self.keys())
            for k,v in self.items():
                tmp[k] = value(len(v))
            return tmp

        return dict.fromkeys(self.keys(), value)

def main(argv=()):

    tmp1 = CustomDict()

    tmp1['n1'] = np.array([[3]])
    tmp1['l2'] = np.array([[ 0,  4],
                            [ 4,  5],
                            [57,  3]])
    tmp1['t3x'] = np.array([[188, 401, 400],
                           [188, 187, 401],
                           [187, 205, 401],
                           [324, 306, 417],
                           [306, 305, 417],
                           [305, 416, 417]])

    # replace values
    print(tmp1.strip(1))

    # replace values with a callable function that gets the size of the array
    print(tmp1.strip(lambda x: [None]*x))

    return 0

if __name__ == "__main__":
    sys.exit(main())

This code outputs:

{'l2': 1, 'n1': 1, 't3x': 1}
{'l2': [None, None, None], 'n1': [None], 't3x': [None, None, None, None, None, None]}

It's all good, it works. But I'm not sure the part that tackles the callable function is the most efficient one, as I am creating the copy of the dictionary first, and then I'm replacing the values with another loop. Can this be done in a single pass?

Why an inherited class?

I don't see anything about this problem that requires an inherited class. strip could just as easily be a free function:

def strip(dct, value=None):
    ...

Also strip isn't a particularly good name - it doesn't really resemble the str.strip() function. I don't know what a better one would be though:

Callable

If you're going to allow passing an arbitrary function, then I think you may as well pass the full value instead of just the length. If the caller wants the length, the caller can use len at that end. This'll enhance all the things you can do.

Also, in Python, EAFP: it's easier to ask for forgiveness than permission. Just try to use the callable as a callable and catch the TypeError:

def strip(dct, value=None):
    try:
        tmp = dict.fromkeys(dct.keys())
        for k,v in dct.items():
            tmp[k] = value(v)
        return tmp
    except TypeError:
        return dict.fromkeys(dct.keys(), value)

Dictionary comprehension

For the except case - fromkeys() makes sense. But in the callable case, not really. You're creating a new dictionary and then re-modifying it. Better to just create the correct dictionary up front:

def strip(dct, value=None):
    try:
        return {k: value(v)
            for k,v in dct.items()
            }
    except TypeError:
        return dict.fromkeys(dct.keys(), value)


filtering element of one List from another in Scala with different tuple structure I am trying to understand the best way to write this. This is part of my organization work so can't disclose it fully here like why such requirement. So Apologies for that.
I have ListA and ListB.
val ListA: List[((String, String), Int)] = List(
  (("A", "B"), 1),
  (("C", "D"), 2),
  (("E", "F"), 5),
  (("E", "F"), 4),
  (("E", "F"), 3)
)
val ListB: List[(String, String)] = List(
  ("A", "B"),
  ("E", "F")
)

We need to search id in ListA that contain ListB element. For example tuple ("A", "B") is present in ListA with (("A", "B"), 1), . so output would be List(1). This process would repeat again once we fetch first match. if no match is found then List(0) would be output. Also both lists are sorted on priority. ListB drives the main priority and after that in case of repeating values the List A does it. And lists are already ordered.
I wrote this.
val result = ((for {
  a <- ListB
  m <- ListA.filter(_._1 == a).map(_._2)
} yield m):::List(0)).slice(0,1)

slice is used because we need List with first match. as ListB is already sorted on priority. Also We are going to repeat this process again by removing already matched id from ListA. But for now please ignore like how we remove these elements and re-iterate.
So new iteration would be on
val ListA: List[((String, String), Int)] = List(
    //  (("A", "B"), 1), This will not be part of second iteration as we already matched it in first iteration.
      (("C", "D"), 2),
      (("E", "F"), 5),
      (("E", "F"), 4),
      (("E", "F"), 3)
    )
    val ListB: List[(String, String)] = List(
      ("A", "B"),
      ("E", "F")
    )

Output of

iteration 1 is List(1)
iteration 2 is List(5)
iteration 3 is List(4)
iteration 4 is List(3)
iteration 5 is List(0)

I would just like to understand that
"Is it right way to code where we search one list element in another".
"Do we have some scala library that can be used here like ListA.filter(ListB.contains(_._1)) --> this needs us to write full filter case method so it wont work like this"

Apart from my comment, I'd suggest using Set for, well sets, since operations will likely be quicker, but more importantly, they're more correct wrt. what operations can be done on them.
val setB: Set[(String, String)] = ListB.toSet

val filtered: List[((String, String), Int)] =
  ListA.filterNot(x => setB.contains(x._1))

val result: List[Int] =
  filtered.map(_._2)

This returns a different result.  As I wrote in my comment I don't quite understand the reasoning behind the algorithm and if it's a real requirement that it's going to be iterative like this, you'll likely have to use the implementation you have right now, i.e. via for.
Also splitting the computation into multiple steps might help with understanding what's going on (especially in a debugger), that's also why I copied over the type signatures as you did.

*If the exact same output is desired*, that'd be this then:
val result = ((for {
  m <- ListA.filter(x => setB.contains(x._1)).map(_._2)
} yield m):::List(0)).slice(0,1)

Nevermind, the same result requires to walk through the list as in the original post.

Also variables shouldn't start with an upper case, c.f. setB instead of SetB.


Copying contents of two files into one file/ bufferedReader close calls I am using the following code to copy the contents of two files into one. Earlier I was not closing the Buff and as eclipse gave warnings I did close it. I want to know if this code is susceptible to failure.

public void mergeFiles() throws IOException{
        ArrayList<String> list = new ArrayList<String>();
        BufferedReader br = null;
        BufferedReader r = null;
        try{
            br = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/BirthdayReminders/fileone.txt"));
            r = new BufferedReader(new FileReader(getApplicationContext().getFilesDir()+"/BirthdayReminders/filetwo.txt"));
            String s1 =null;
            String s2 = null;

            while ((s1 = br.readLine()) != null){
                list.add(s1);
            }
            while((s2 = r.readLine()) != null){
                list.add(s2);
            }
        }
        catch (IOException e){
            e.printStackTrace();
            FLAG =1;
        }finally{
            br.close();
            r.close();
        }

        BufferedWriter writer=null;
        writer = new BufferedWriter(new FileWriter(getApplicationContext().getFilesDir()+"/BirthdayReminders/output.txt"));
        String listWord;
        for (int i = 0; i< list.size(); i++){
            listWord = list.get(i);
            writer.write(listWord);
            writer.write("\n");
        }

        writer.close();
    }

Should I check if br is null in the finally block - my intuition tells me no, because finally will be only executed when try is. I have something over Here which is contradictory to my belief in declaring the variables before try block. What is the best way to do this?

A little bit about code style: it is conventional to surround binary operators with whitespaces(for instance, getApplicationContext().getFilesDir()+"/BirthdayReminders/fileone.txt" should be  getApplicationContext().getFilesDir() + "/BirthdayReminders/fileone.txt") and there should be a whitespace after a closing bracket and before the opening one(that is, } finally { is better than }finally{ and so on).
Yes, you should check that br and r are not null before calling the close method on them. The FileReader constructor can throw an exception. In this case they will be null.  However, you can use try-with-resources(if you have Java 7 or later) to make your code more simple and concise:

try (
    BufferedReader reader1 = new BufferedReader(new FileReader(...));
    BufferedReader reader2 = new BufferedReader(new FileReader(...))
) {
    // do something
} catch (IOException e) {
    // do something else
}

Variable names: r and br are not really descriptive(however, their scope is very narrow so it is not a very big deal in this case). The same holds true for other variables: I would use something like lines instead of list and so on.
This kind of initialization is pointless in my opinion:

BufferedWriter writer = null;
writer = new BufferedWriter(new FileWriter(getApplicationContext().getFilesDir() + "/BirthdayReminders/output.txt"));

This one looks better:

BufferedWriter writer = new BufferedWriter(new FileWriter(getApplicationContext().getFilesDir() + "/BirthdayReminders/output.txt"));

The listWord variable and an index-based loop is redundant:

for (String listWord: list) {
    // do something
}

For-each loop is shorter and simpler.


Replacing removed secure empty trash feature Is this code reliable? Is there any extra precaution I should or should not be taking here? How is my code to securely delete a file by repeatedly overwriting the contents?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main(int Argc, const char *Argv[]) {
    srand(time(NULL));
    unsigned int *RandBuf = NULL;
    for (int I = 1; I < Argc; I++) {
        long FileSize;
        const char *const Path = Argv[I];
        FILE *const File = fopen(Path, "r+");
        if (!File || fseek(File, 0, SEEK_END) || (FileSize = ftell(File)) < 0) {
            perror(Path);
            continue;
        }
        const unsigned long IntCount = (FileSize+3)>>2;
        RandBuf = realloc(RandBuf, IntCount<<2);
        const unsigned char WriteCount = ((unsigned int)rand()&0xF)+16;
        for (unsigned char Write = 0; Write < WriteCount; Write++) {
            for (unsigned long Int = 0; Int < IntCount; Int++) {
                RandBuf[Int] ^= (unsigned int)rand();
            }
            if (fseek(File, 0, SEEK_SET) || fwrite(RandBuf, 1, FileSize, File) < FileSize) {
                perror(Path);
                continue;
            }
        }
        if (fclose(File) || remove(Path)) {
            perror(Path);
            continue;
        }
    }
    free(RandBuf);
    return 0;
}

Your description appears to suggest you're reimplementing wipe; I recommend you read its sources to see what an effective implementation looks like.  I'll note that the concept could well be flawed if you're writing to a log-structured filesystem or to wear-levelling media - both of those need specific actions to erase data, because overwriting will simply create new data blocks elsewhere.
The "PascalCase" naming convention makes this code surprisingly hard to read (and not just because capital I looks a lot like 1, and Int sounds a lot like int).  I would prefer to see more conventional identifiers, so that variables look like variables.
This include looks pointless:

 #include <unistd.h>

As we're using only Standard Library, prefer to omit this, for a portable program.

    if (!File || fseek(File, 0, SEEK_END) || (FileSize = ftell(File)) < 0) {
        perror(Path);
        continue;
    }

I approve of error checking, but there's a problem here.  If we open a file but fail to seek (because it's a non-regular file, perhaps), then we fail to close it.

    RandBuf = realloc(RandBuf, IntCount<<2);

What happens when realloc() returns a null pointer?  We have a leak and we have undefined behaviour when we dereference RandBuf.  It seems risky to need that much memory, given that files can often be larger than address space.  We'd probably prefer to write in sensible size chunks too, given the copying that's involved.

    const unsigned long IntCount = (FileSize+3)>>2;
    RandBuf = realloc(RandBuf, IntCount<<2);

Where do those magic numbers 2 and 3 originate?  I'm guessing that perhaps that sizeof (int) is 4 on your platform, and the constants derive from that.  But it would be better to calculate from the actual size of int and to name the constants more appropriately.

    for (unsigned char Write = 0; Write < WriteCount; Write++) {

This loop serves no purpose.  We write a variety of values several times, but it's unlikely any of these values go all the way to the disk, given we never fflush() the stream.


jQuery AJAX submit form This jQuery code submits a form using AJAX. The server response should return JSON.

addNewOutreach = function(node)
    {
    var form = node.find('form');
    var data = form.serialize();

    $.post('ajax.php',
    {action:'addNewOutreach',string:data},
    function(response) {
        if(response!=null && response.status=='success')
            {
            message_return('success',response.message);
            unfreezeModal(node);
            node.modal('hide');
            }
        else if(response!=null && response.status=='error')
            {
            message_return('error',response.message);
            unfreezeModal(node);
            }
        else
            {
            message_return('error','Error saving outreach.');
            unfreezeModal(node);
            }
        },'json');
    }

message_return() is a function that displays feedback on the page.

unfreezeModal() is a function that locks the form (avoiding double submits) until the request is finished.

Where this code lacks:

What if the server does not respond with valid JSON?
What if the AJAX request keeps hanging?
The function(response) block can probably be isolated from this block so it can be used in other AJAX request
Can probably be written cleaner with Promises

You should first check if the response is null. If not, then check for a success or error response.
You can use a try-catch block to check if it is valid JSON:

Try{
    JSON.parse(response);
    catch(Exception){
    console.log("Not valid json");
    handle();
}

If what you are expecting from the server to be in a constant format you could use the callback code again and again in different functions.


Initializing an array of relative coordinates of all adjacent 2D matrix cells The program should output this:

_1 _1
_1  0
_1  1
 0 _1
 0  1
 1 _1
 1  0
 1  1

Currently I have this code:

(4 ~: i.9) # (3 # i:1) ,. 9 $ i:1

But I think it can be better.

Your solution relies excessively on magic numbers, such as 9 and 3.  It also obscures the Cartesian-product aspect of the problem.  Therefore, I think it could be better written as:

(< (< (< 4))) { ,/ > { ;~i:1

Derivation

(i:1) produces the list _1 0 1.
{ (i:1) ; (i:1) produces the Cartesian product:

┌─────┬────┬────┐
│_1 _1│_1 0│_1 1│
├─────┼────┼────┤
│0 _1 │0 0 │0 1 │
├─────┼────┼────┤
│1 _1 │1 0 │1 1 │
└─────┴────┴────┘

@earl points out that this reflexive expression can be written more succinctly as {;~i:1.
> unboxes it into a 3 × 3 × 2 matrix, and ,/ flattens it into a 9 × 2 matrix:

_1 _1
_1  0
_1  1
 0 _1
 0  0
 0  1
 1 _1
 1  0
 1  1

The only remaining task is to exclude the 0 0 row, which is element 4.  The (< (< (<4))) { selector seems to do this job.


Stack having push, pop and return min in O(1) I am using a class which is taking care of minimum value so far. Please let me know of any improvements or suggestions.

class MinStack {

    Stack<Node> s1 = new Stack<>();

    public void push(int x) {
        if(s1.isEmpty()) {
            s1.push(new Node(x,x));
        } else {
            s1.push(new Node(x,Math.min(s1.peek().min, x)));
        }

    }

    public void pop() {
        s1.pop();
    }

    public int top() {
        return s1.peek().val;
    }

    public int getMin() {
        return s1.peek().min;
    }

    static class Node{
        int val;
        int min;
        public Node(int v, int m) {
            val = v;
            min = m;
        }
    }
}

The general algorithm is great. At first, I thought you were overwriting the value with the min value, though, and it's because the fact that the Node stores two values is not very visible. A comment would help a lot here, and separating the logic in to a few lines too. This line:

s1.push(new Node(x,Math.min(s1.peek().min, x)));

should be:

int previousMin = backingStack.peek().minSoFar;
int minSoFar = Math.min(previousMin, value);
backingStack.push(new Node(value, minSoFar));

Note that I have also renamed all your variables. x is a poor name for a value. An x is a coordinate. s1 is another poor name, because where is s0, s2, etc... and what does s mean? min is not horrible, but it gives no indication of what it is the minimum of... what the context is.

Then, why are you using a Stack as the back-end implementation? I know it seems logical to use java.util.Stack, but you should know that it is not a recommended class any more. Stacks should be implemented over something that implements the Deque interface like java.util.LinkedList. The existing Stack class is bad because it is based on top of Vector which is a synchronized class that has a few implementation issues that are not recommended. The Stack class has this to say: "A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class.". The Vector class has this to say: "Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector."

So, Stack is bad. I would use:

private final Deque<Node> stack = new LinkedList<>();

Now, about the pop() method. It is a very C-like thing to split the pop() and "get" methods. Java, and most other languages, return the top value with the pop() call and avoid the need for a second call. Your pop() should return a value. You also need an isEmpty() Method so that people know when they have run out of data before they get a NoSuchElementException.

Oh, and the Node class can be private.

So, something like:

public class MinStack {

    private final Deque<Node> stack = new LinkedList<>();

    public void push(int value) {
        if(stack.isEmpty()) {
            stack.addFirst(new Node(value, value));
        } else {
            int minSoFar = Math.min(value, stack.getFirst().stackMin);
            stack.push(new Node(value, minSoFar));
        }
    }

    public boolean isEmpty() {
        return stack.isEmpty();
    }

    public int pop() {
        return stack.removeFirst().value;
    }

    public int peek() {
        return stack.getFirst().value;
    }

    public int getMin() {
        return stack.getFirst().stackMin;
    }

    private static class Node{
        int value;
        int stackMin;
        public Node(int val, int min) {
            value = val;
            stackMin = min;
        }
    }
}


Implementing a generic class and choosing a collection based on a type In this topic here I have aksed for help to sort out an error while creating a generic class whose method can return a different collection given a type. I received the answer and working solution but I have a feeling the problem is not sorted in proper way. That is why I came here for a review.

The reason for having a generic class is is that I use DI and pass either DbContext to collect data from a database or another Object to "collect" data from collections. The DI is done in the main Program. The interface is generic to handle DbContext and then fetching particular collection given a type: DbContext.Set<>(). The FetchDataFromCollection is also generic, so is FetchDataFromDatabase because this is how DbCOntext works.

Is there any other way to sort it out and for instance avoid if ... else blocks? I was thinking to make another class ModelBase, inherits from it into concrete implementations, which would return a specific collection and inject such class into FetchFromCollection but then I would have to make this decition by myself which class to inject to get proper collection in FetchFromCollection. What I would have is a method which would decide to return a particular collection given a type.

I don't know if it's possible. Below are the three program components.

IFetchData:

public interface IFetchData<TEntity>
{
    IEnumerable<TEntity> GetItems();
}

Then I have created these two classes:

FetchFromDatabase:

public class FetchFromDatabase<TEntity> : IFetchData<TEntity>
    where TEntity : class
{
    private readonly MainDBContextBase context;

    public FetchFromDatabase(MainDBContextBase context)
    {
        if (context == null)
            throw new ArgumentNullException("DB context");
        this.context = context;
    }

    public IEnumerable<TEntity> GetItems()
    {
        return context.Set<TEntity>();
    }
}

FetchFromCollection:

public class FetchFromCollection<TEntity> : IFetchData<TEntity>
    where TEntity : class
{
    // ModelBase<TEntity> models;

    private readonly InitializeComponents components;
    private ModelModules modules;
    private ModelSpecializations specializations;
    private ModelTeachers teachers;
    private ModelStudents students;

    public FetchFromCollection(InitializeComponents components)
    {
        if (components == null)
            throw new ArgumentNullException("Context");
        this.components = components;
    }

    public IEnumerable<TEntity> GetItems()
    {
        if (typeof(TEntity) == typeof(Module))
        {
            if (modules == null)
                modules = new ModelModules(components);
            return (IEnumerable<TEntity>)modules.Set();
        }
        if (typeof(TEntity) == typeof(Specialization))
        {
            if (specializations == null)
                specializations = new ModelSpecializations(components);
            return (IEnumerable<TEntity>)specializations.Set();
        }
        if (typeof(TEntity) == typeof(Module))
        {
            if (teachers == null)
                teachers = new ModelTeachers(components);
            return (IEnumerable<TEntity>)teachers.Set();
        }
        else
        {
            if (students == null)
                students = new ModelStudents(components);
            return (IEnumerable<TEntity>)students.Set();
        }
    }
}

It seems like you could just mirror the way FetchFromDatabase is implemented. First thing you will need is a something like a DbContext, but for collections. This new class should be capable of returning an IEnumerable<TEntity> for any TEntity defined -- just like DbContext.

public class CollectionContext
{
    private Dictionary<Type, object> _cache;

    public CollectionContext()
    {
        _cache = new Dictionary<Type, object>();
    }

    public void AddCollection<TEntity>(IEnumerable<TEntity> collection)
    {
        _cache.Add(typeof(TEntity), collection);
    }

    public IEnumerable<TEntity> Set<TEntity>()
    {
        return (IEnumerable<TEntity>)_cache[typeof(TEntity)];
    }
}

Now FetchFromCollection can be implemented exactly like FetchFromDatabase, with exception of the type used in the constructor.

public class FetchFromCollection<TEntity> : IFetchData<TEntity>
where TEntity : class
{
    private readonly CollectionContext context;

    public FetchFromCollection(CollectionContext context)
    {
        if (context == null)
            throw new ArgumentNullException("Collection context");
        this.context = context;
    }

    public IEnumerable<TEntity> GetItems()
    {
        return context.Set<TEntity>();
    }
}

This is how you'd use it.

// Load up all your collections
var context = new CollectionContext();
context.AddCollection<Module>(new ModelModules(components).Set());
context.AddCollection<Specialization>(new ModelSpecializations(components).Set());
context.AddCollection<Teacher>(new ModelTeachers(components).Set());

// Example usage
IFetchData<Module> modules = new FetchFromCollection<Module>(context);

I think you can further improve on your design by getting rid of the generic type parameter on FetchFromDatabase and FetchFromCollection. Each of these classes is capable of pulling any TEntity out of their contexts'. By making this change you'd only need to have one instance of FetchFromDatabase and FetchFromCollection for your entire application, instead of one instance of each for every TEntity. You can just move the generic type parameter down to GetItems(). This same change would need to be made to IFetchData. Here is an example for FetchFromCollection, the same change can be made to FetchFromDatabase:

public class FetchFromCollection : IFetchData
   {
    private readonly CollectionContext context;

    public FetchFromCollection(CollectionContext context)
    {
        if (context == null)
            throw new ArgumentNullException("Collection context");
        this.context = context;
    }

    public IEnumerable<TEntity> GetItems<TEntity>()
    {
        return context.Set<TEntity>();
    }
}


String parsing with multiple delimeters My data is in this format:

  龍舟 龙舟 [long2 zhou1] /dragon boat/imperial boat/\n

And I want to return:

('龍舟', '龙舟', 'long2 zhou1', '/dragon boat/imperial boat/')

In C I could do this in one line with sscanf, but I seem to be  f̶a̶i̶l̶i̶n̶g̶ writing code like a schoolkid with Python:

    working = line.rstrip().split(" ")
    trad, simp = working[0], working[1]
    working = " ".join(working[2:]).split("]")
    pinyin = working[0][1:]
    english = working[1][1:]
    return trad, simp, pinyin, english

Can I improve?

You can use Regular Expressions with re module. For example the following regular expression works with binary strings and Unicode string (I'm not sure which version of Python you use).

For Python 2.7.3:

>>> s = "龍舟 龙舟 [long2 zhou1] /dragon boat/imperial boat/\n"
>>> u = s.decode("utf-8")
>>> import re
>>> re.match(r"^([^ ]+) ([^ ]+) \[([^]]+)\] (.+)", s).groups()
('\xe9\xbe\x8d\xe8\x88\x9f', '\xe9\xbe\x99\xe8\x88\x9f', 'long2 zhou1', '/dragon boat/imperial boat/')
>>> re.match(r"^([^ ]+) ([^ ]+) \[([^]]+)\] (.+)", u).groups()
(u'\u9f8d\u821f', u'\u9f99\u821f', u'long2 zhou1', u'/dragon boat/imperial boat/')

For Python 3.2.3:

>>> s = "龍舟 龙舟 [long2 zhou1] /dragon boat/imperial boat/\n"
>>> b = s.encode("utf-8")
>>> import re
>>> re.match(r"^([^ ]+) ([^ ]+) \[([^]]+)\] (.+)", s).groups()
('龍舟', '龙舟', 'long2 zhou1', '/dragon boat/imperial boat/')
>>> re.match(br"^([^ ]+) ([^ ]+) \[([^]]+)\] (.+)", b).groups()
(b'\xe9\xbe\x8d\xe8\x88\x9f', b'\xe9\xbe\x99\xe8\x88\x9f', b'long2 zhou1', b'/dragon boat/imperial boat/')


Optimizing large file reads with a custom line terminator in Python I asked for help accomplishing this on StackExchange.
How to efficiently read a large file with a custom newline character using Python?

And ended up providing my own answer which is duplicated here.

class ChunkedFile(object):
    """
    Yields file in 100 megabyte chunks (by default).
    If lineterminator is provided reads through file in chunks but yields lines.
    """

    def __init__(self, file_to_process, lineterminator=None):
        self._file = file_to_process
        self._lineterminator = lineterminator

    def read(self, encoding="utf-8", chunk_size=None):
        one_hundred_megabytes = 100 * 1024
        chunk_size = chunk_size or one_hundred_megabytes

        with self._file.open(encoding=encoding) as f:
            chunk = ""
            while True:
                data = f.read(chunk_size)
                if not data:  # EOF
                    break

                chunk += data
                if self._lineterminator is None:
                    yield chunk
                    chunk = ""

                elif self._lineterminator in chunk:
                    lines = chunk.split(self._lineterminator)
                    chunk = "" if chunk.endswith(self._lineterminator) else lines.pop()
                    for line in lines:
                        if line:  # Don't return empty lines.
                            yield line

if __name__ == "__main__":
    longabstract_file = Path(__file__).parent / "longabstract_en.csv"
    file_chunk_reader = ChunkedFile(longabstract_file, lineterminator="\tl\n")
    for line in file_chunk_reader.read():
        print(line)

The code works but I'm interested in further optimizations and proper error handling. Nothing broke when I ran it on a few 1GB+ files so it's a start!

PS - I prefer pure Python 3 code (if relevant). Thanks.

1. Bug

If the file does not end with the line terminator, then the last line is lost. That's because the code says:

if not data:  # EOF
    break

but at this point there might still be an unterminated line remaining in chunk, so this needs to be:

if not data:  # EOF
    if chunk:
        yield chunk
    break

Perhaps in your case the file is required to always end with a line terminator, but if so it would be a good idea to assert this, rather than silently discarding data if the file does not meet the requirement:

if not data:  # EOF
    if chunk:
        raise ValueError("file does not end with line terminator")
    break

2. Separation of concerns

The code in the post has four concerns:

Opening and closing a file.
Generating chunks of 100 kilobytes from a file.
Generating lines from a file with arbitrary line terminator.
Filtering out blank lines.

The principle of separation of concerns suggests that these should not be combined into one function. Taking the four concerns in turn:

As a general rule, if you have a function that operates on the contents of a file, it's better if the function takes a file-like object (not a filename) and leaves the opening and closing of the file up to the caller. That's because not all file-like objects correspond to named files. The caller might have a pipe from a call to subprocess.Popen, or they might have some data in memory wrapped as a io.StringIO. If an interface takes a filename and opens the file itself, then it can't be used with these other kinds of file-like object.
Reading fixed-size chunks from a file is already implemented by the read method.
This is fine, but see below for some improvements.
Filtering out blank lines might not be needed or wanted, so it should be left to the caller to decide whether to do it. It is in any case easy to implement using an if statement, or a comprehension, or filter.

3. Other points

There are no docstrings.
When you have a class with __init__ and one other method, then there is usually no need for a class: what you want is a function. (See Jack Diederich's PyCon 2012 talk "Stop Writing Classes" for a detailed look at this issue.)
100 × 1024 bytes is one hundred kilobytes (not megabytes).
Instead of having a keyword argument with default value of None:

def read(..., chunk_size=None):

and then later setting it to a particular value if it tests false:

one_hundred_megabytes = 100 * 1024
chunk_size = chunk_size or one_hundred_megabytes

just specify the keyword argument with the default value you want:

def read(..., chunk_size=100 * 1024):

The code appends to chunk before testing for the line terminator:

chunk += data
# ...
elif self._lineterminator in chunk:

but we know that if the line terminator appears, then it must appear in the new part of the chunk (that we just read). It can't appear in the old part because then it would have been split on the previous iteration. This means that if multiple chunks need to be read before the line terminator appears, then the initial part of the line gets searched again each time. This leads to quadratic runtime complexity if lines are much longer than the chunk size.

For efficiency, we should test self._lineterminator in data instead.
The code tests for the line terminator appearing in the chunk and then splits the chunk if it is found.

elif self._lineterminator in chunk:
    lines = chunk.split(self._lineterminator)

but this has to search the chunk twice, once for the in operator, and again for the split. It would be faster to split first and then see how many pieces there are:

lines = chunk.split(self._lineterminator)
if len(lines) > 1:

Combining points 5 and 6 above, we should split data first and append to chunk afterwards:

first, *rest = data.split(self._lineterminator)
chunk += first
if rest:
    yield chunk
    yield from rest[:-1]
    chunk = rest[-1]

I think the names would be clearer if chunk were renamed part (because it is part of a line), and data were renamed chunk (because it is a chunk of text read from the file).

4. Revised code

def splitlines(file, newline, chunk_size=4096):
    """Generate lines from file (a file-like object), where a line is
    followed by the string newline (or the end of the file). Optional
    argument chunk_size is the size of the chunks read from the file.

    """
    part = ''
    while True:
        chunk = file.read(chunk_size)
        if not chunk:
            if part:
                yield part
            break
        first, *rest = chunk.split(newline)
        part += first
        if rest:
            yield part
            yield from rest[:-1]
            part = rest[-1]

The caller should write something like:

with open(longabstract_file) as f:
    for line in splitlines(f, "\tl\n"):
        if line: # ignore blank lines
            print(line)


Computer Science glossary made with Tkinter This is a Computer science glossary that I made with Tkinter. The user inputs the word that they want and then the code searches the text file for it and then ouputs it to the user.

#My Computer Scienece Glossary project

from tkinter import *

#key down function
def click():
    global definition
    entered_text=textentry.get() #this will collect the text from the text entry box
    output.delete(0.0, END)
    try:
        definition = my_compdictonary[entered_text]
    except:
        definition = "Sorry defintion not found"
    output.insert(END, definition)

### main:
window = Tk()
window.title("Computer Science Glossary")
window.configure(background="black")

### My Photo
photo1 = PhotoImage(file="me.gif")
Label(window,image=photo1, bg="black") .grid(row=0, column=0, sticky=W)

#create label
Label(window, text="Enter the word you want the definition for:",bg="black", fg="white", font="none 12 bold").grid(row=1, column=0, sticky=N)

#create a text entry box
textentry = Entry(window, width=20, bg="white")
textentry.grid(row=2, column=0, sticky=N)

#add a submit button
Button(window, text="SUBMIT", width=6, command=click).grid(row=3, column=0, sticky=N)

#create another label
Label(window, text="\nDefinition:", bg="black", fg="white", font="none 12 bold").grid(row=4, column=0, sticky=N)

#create a textbox
output = Text(window, width=60, height=4, wrap=WORD, background="white")
output.grid(row=5, column=0, columnspan=1, sticky=N)

#the dictionary
my_compdictonary = {}
with open("glossary.txt", "r+") as file:
    for line in file:
        word, defintions = line.strip().split(" : ")
        my_compdictonary[word] = defintions

window.mainloop()

I want suggestions on how I can make this computer science glossary better.

All suggestions will be greatly appreciated.

PEP-8

As always, follow the PEP-8 coding standards.

Don't exceed 79 characters per line
You a space around the = assignment operator (you mostly do this, but violate it in entered_text=textentry.get()

Use a main function

Avoid writing code in the main scope.  Move the code into a main function, and call it ... but only if the script is not imported into another file:

def main():
    window = Tk()
    # ...
    window.mainloop()

if __name__ == '__main__':
    main()

Catch only the exceptions you expect

The following code:

try:
    definition = my_compdictonary[entered_text]
except:
    definition = "Sorry definition not found"

will catch any and every exception, and ignore them all, regardless of the correct type.  Why is it bad?  Imagine you correct the spelling error in your main program, and change my_compdictonary to my_compdictionary, but you don't correct it in this section of code.  You test the program, and it doesn't crash, but every definition you look up returns "Sorry definition not found".  Where is the bug?  How hard is it to debug?

If instead the code was written as:

try:
    definition = my_compdictonary[entered_text]
except KeyError:
    definition = "Sorry definition not found"

the program would crash with a NameError, with the traceback showing you the exact line in question.  Now how hard is it to debug?

In this particular case, the try...except structure is really overkill.  You can provide a default argument when getting a key from the dictionary to handle the key not found case.  That reduces the above four lines to only one:

definition = my_compdictonary.get(entered_text, "Sorry definition not found")

Keep related code together

You have:

output.delete(0.0, END)
# Four
# unrelated
# lines of
# code
output.insert(END, definition)

It would be clearer to keep output.delete() and output.insert() as back to back lines.  Eg)

definition = my_compdictonary.get(entered_text, "Sorry definition not found")
output.delete(0.0, END)
output.insert(END, definition)

Avoid global

Avoid using global as much as possible.  It is a sign of poorly organized code, and makes it harder to reason about the correctness of the code, and harder to test the code because side-effects increase in scope.

In this case, it is simple to avoid the global definition because definition is never used anywhere except in the click() function.  It should not be a global variable; it is local!

Never ask for more than you need

with open("glossary.txt", "r+") as file:

Why are you opening the file for reading plus?  Will you ever be writing to it?  Can the program now accidentally write to the file because it has write privileges, corrupting the file?  You are only reading the file; just open it for reading.

with open("glossary.txt") as file:

Be liberal in what you accept

The Robustness Principle warns us about the input we get from the outside.  Be fault tolerant.  Or perhaps better, make it so faults can't happen.

    word, defintions = line.strip().split(" : ")

What happens when a definition contains a colon?

noun : A person, place, or thing.  Can be classified into two groups : common or proper.

split(" : ") now returns 3 items, and you've only got variables for two.  So you'll get ValueError: too many values to unpack (expected 2).  How can you avoid this?  How about telling split() that you only expect to find one delimiter:

    word, defintions = line.strip().split(" : ", 1)

Now, word is "noun" and defintions is "A person, place, or thing.  Can be classified into two groups : common or proper."

What about blank lines?  They will still crash the program.  You might want to test for those as well.  What about non-blank lines that don't have a definition?  Those, well maybe it would be ok if those result in an exception; that would be really bad input in the "glossary.txt".

Make it easier for reviewers to run your code.  You have an image 'me.gif' in your code that we don't have.  Running without it crashes the program due to file not found.  How big was that image?  32x32?  300x300?  It would affect the size of the resulting window.  Do we just delete the lines of code so we can test the program?

How about 'glossary.txt'?  Do we have to guess what the file format is?  Perhaps you could have given a sample file with 3 or 4 entries.

Future Improvements

You might want to allow the user to look up a definition by just pressing "Enter" after typing in the word.  The way it works right now, the user has to move their hands from the keyboard to the mouse.

What if the user enters with CAPS LOCK on?  What if the definition is provided with a capital letter?  Is the definition really not found?  Could you be a little more liberal in what you accept?

Here is a simple refactoring of your original code with some of the suggestions from comments above.  No global variables.  The click() function is moved into main(), so it has access to the local variables textentry, my_compdictonary and output.  The image was deleted, so I could quickly run the program.  The dictionary is generated using list comprehension, and will ignore blank lines.

This is by no means the "best" code.  Many improvements can still be made in naming (can we have something better than click?), spelling of variables, and so on.  Perhaps a class may be in order.

from tkinter import *

"""
A Computer science glossary made with Tkinter

Requires a "glossary.txt" file with definitions, one per line, with a
space-colon-space separating the term from the definition.  Eg)

    word : a fragment of language
    term : a word or phrase used to describe a thing or to express a concept

Also should require a "me.gif" image of approximately 64 x 64 pixels, but
that was removed temporarily.
"""

def main():
    """Main function.

       This function creates the UI, and enters the tkinter mainloop.
       It does not return until the user closes the UI."""

    def click():
        entered_text = textentry.get()
        definition = my_compdictonary.get(entered_text,
                                          "Sorry defintion not found")
        output.delete(0.0, END)
        output.insert(END, definition)

    window = Tk()
    window.title("Computer Science Glossary")
    window.configure(background="black")

    Label(window, text="Enter the word you want the definition for:",
          bg="black", fg="white", font="none 12 bold"
         ).grid(row=1, column=0, sticky=N)

    textentry = Entry(window, width=20, bg="white")
    textentry.grid(row=2, column=0, sticky=N)

    Button(window, text="SUBMIT", width=6, command=click).grid(row=3, column=0, sticky=N)

    Label(window, text="\nDefinition:", bg="black", fg="white",
          font="none 12 bold"
         ).grid(row=4, column=0, sticky=N)

    output = Text(window, width=60, height=4, wrap=WORD, background="white")
    output.grid(row=5, column=0, columnspan=1, sticky=N)

    with open("glossary.txt", "r") as file:
        my_compdictonary = dict( (line.split(" : ", 1)) for line in file if line )

    window.mainloop()

if __name__ == '__main__':
    main()


"Bulls and cows" algorithm Bulls and cows is a variation of the popular game called 'Mastermind', On a sheet of paper, the players each write a 4-digit secret number. The digits must be all different. Then, in turn, the players try to guess their opponent's number who gives the number of matches. If the matching digits are on their right positions, they are "bulls", if on different positions, they are "cows"

Wikipedia page

One method is to generate a list of all possible numbers that could be the answer, then to prune the list by keeping only those numbers that would give an equivalent score to how the last guess was scored. The next guess can be any number from the pruned list.

Either the program guess correctly or run out of numbers to guess, which indicates a problem with the scoring.

This is the implementation for n digits in C++, hunters (the list of all possible numbers that could be the answer) is a vector of string and cows it he function that counts the cows from two numbers:

for(int h=0; Hunters.size() > 1; h++){
    askBullsCows(h);
    string guess = Hunters.at(0);
    for(int i=Hunters.size()-1; i >= 0; i--){
        int pl = 0, fl = 0;
        for (unsigned int ix = 0; ix < Hunters.size(); ix++){
            if(Hunters[i].at(ix) == guess.at(ix))    pl++;
        }
        Cows(i, guess, fl);
        if((pl != P.p) || (fl != P.f)){
           Hunters.erase(cazadores.begin()+i);}
    }
}

Now, hunters (the vector) have more than 150.000 strings and it's too expensive for erase one by one string that couldn't be the answer.

Use char[n] instead of string to store each individual solution.
Copy just the eligible solutions to a brand new vector (as Ben Voigt said) instead of deletion.

Assume n<=10. So, memory you need is less than 73Mb (i.e. twice 10*10!). Sequential memory access (#2) and avoiding pointer dereference (#1) means high performance on modern DDR memory.

UPDATE: Some notes on random or even non-random, but non-sequentional DDR memory read. Say, four 8-byte reads at addresses 0x8000 0x8100 0x8200 0x8300 will be slower than four 8-byte reads at 0x9000 0x9008 0x9010 0x9018, because in the later case all the data for the last reads (i.e. 0x9008 0x9010 0x9018) are already in processor 32-byte cache line (also, there are 64-byte cache lines in some processors). Each memory read (say, 8-byte read) transfers the whole 32-byte cache line from memory to processor cache.

Next, even the best possible optimizer can not decrease non-sequentional memory access latency (that is, the time between the memory address was calculated and the memory address contents was arrived into the processor)


Given a sorted array nums, remove the duplicates in-place I wrote an algorithm to solve this challenge but would love some feedback on its efficiency and, if you were an interviewer, what your opinion would be on my solution. I believe my time complexity is \$O(N)\$. Is this correct?

The Task:

  Given a sorted array nums, remove the duplicates in-place such that
  each element appear only once and return the new length.

  Do not allocate extra space for another array, you must do this by
  modifying the input array in-place with O(1) extra memory.

    Example:
    Given nums = [0,0,1,1,1,2,2,3,3,4]

    Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

    It doesn't matter what values are set in the array beyond the returned length.

My Solution:

def removeDuplicates(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    if(nums):
        IndFirstKind = IndCompare = 0
        lengthArray = len(nums)
        while(IndCompare < lengthArray):
            if nums[IndFirstKind] == nums[IndCompare]:
                IndCompare = IndCompare + 1
            else:
                if(IndFirstKind + 1 != IndCompare):
                    temp = nums[IndCompare]
                    nums[IndCompare] = nums[IndFirstKind + 1]
                    nums[IndFirstKind + 1]= temp
                IndFirstKind = IndFirstKind +1
                IndCompare = IndCompare + 1
        return IndFirstKind + 1
    return 0

This is a job for itertools.groupby:

i = 0
for key, _ in groupby(nums):
    nums[i] = key
    i += 1
return i

A look at the implementation of groupby shows that it allocates \$O(1)\$ extra space, as required by the problem.

The i += 1 could be avoided using enumerate, like this:

i = -1 # in case nums is empty
for i, (key, _) in enumerate(groupby(nums)):
    nums[i] = key
return i + 1

but the need for an exceptional case to handle empty input is rather inelegant.


Simple Age Calculator I have written an age calculator that takes a birthDate as input.

I'd like a general review of this. I'm especially concerned about the message variable and the lines after the try/catch statement.

namespace Age
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    Console.Write("Enter your birtdate: ");
                    DateTime birthDate = DateTime.Parse(Console.ReadLine());

                    int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear);
                    int Years = Days / 365;
                    string message = (Days >= 365) ? "Your age: " + Years + " years" : "Your age: " + Days + " days";

                    Console.WriteLine(message);
                }
                catch
                {
                    Console.WriteLine("You have entered an invalid date.\n");
                }

                Console.WriteLine("Exit? (y/n)");
                string userValue = Console.ReadLine();

                if (userValue == "y")
                {
                    Environment.Exit(0);
                }
            }
        }
    }
}

I was going to edit my other answer, but instead I'm going to take a different approach - I mean, I'll push it to the extreme ;)

Warning
I'll push it to the extreme is to be taken literally. This solution doesn't aim at solving the simple age calculator problem, rather at showing how one would architect a SOLID application - if the goal is just to calculate the difference between two dates, this is absolute overkill. If the goal is to learn how to write good OOP using a trivial/simple problem as an excuse...
Buckle up, you're in for a ride.

static void Main(string[] args)
As you know by now, this is your application's entry point. When this static method returns, the program ends. To terminate your program, simply use return; in this method, or structure your program flow so that normal exit simply causes the main thread (imagine a cursor running each instruction sequentially - or step through your code in the debugger) to reach the bottom of the Main method.
This method being static, if you're going to call anything outside of it, it's going to have to be static as well. If you're writing procedural code, it doesn't matter.
If you want object-oriented code, the Main method will probably have a very high level of abstraction, and will read like pseudo-code, if not like plain English.
The method revolves around the idea that it's a program's birth, life, and death.
Dependency Injection (DI) disciples (guilty!) call this entry point, the composition root. This is where you instantiate the application (and its dependencies), and run it.
Simplified to the extreme:
static void Main(string[] args)
{
    var app = new MyApplication();
    app.Run();
}

What's in the Run() method?
public class MyApplication
{
    public MyApplication()
    {
        // initialisation here
    }

    public void Run()
    {
        // app logic here
    }
}

Notice that the Run method is not static. It exists only as a member of the interface of an object defined by this MyApplication class - in other words, you need an instance of MyApplication to call this method.
In this case the application logic part will feature our main loop, from which we will exit based on a condition.
public void Run()
{
    var keepRunning = true;

    while(keepRunning)
    {
        // application logic
        keepRunning = false; // exit
    }
}

At this point, we've reached a point of no return. Anything else we code in the Run method will impact maintainability, testability, and readability. Better keep it to a minimum.

The key resides in delegating the work. The MyApplication class cannot do its work alone without using the new keyword, which would increase coupling, and without doing the work all by itself, which would decrease cohesion. Since we want low coupling and high cohesion, we'll start by avoiding the use of static methods and of the new keyword.

Why?
Good code is testable code. You'll want to be able to write tests for the code you write - whether you write these tests or not, writing testable code will tend to produce code that is more cohesive and less coupled.
See also: https://stackoverflow.com/a/3085419/1188513

IUserInteraction
We know we want to use the console to interact with the user, but in order to test our application logic, we'll want to be able to substitute the user's input for whatever our tests need.
public interface IUserInputProvider
{
    string GetUserInput(string prompt);
    T GetUserInput<T>(string prompt, IUserInputValidator<T> validator);
}

With that - and that only, we already have enough to go back to the MyApplication class:
public class MyApplication
{
    private readonly IUserInputProvider _inputProvider;

    public MyApplication(IUserInputProvider inputProvider)
    {
        _inputProvider = inputProvider;
    }

    public void Run()
    {
        var keepRunning = true;

        while(keepRunning)
        {
            var prompt = "Enter your birth date:";
            var input = _inputProvider.GetUserInput(prompt); // no validation for now
            // ...

            keepRunning = false; // exit
        }
    }
}

I'll get back to the IUserInputValidator<T> later.

Mocking
GetUserInput is a method that returns a string. Nothing more, nothing less. We know that we want to call Console.ReadLine(), but that's an implementation detail that the MyApplication class does not need to know about.
If we were to write a test to see if the Run method effectively exits when the user enters "y", we would not bring up a console and wait for someone to enter "y" - instead we would set up a mock - a "fake" implementation of the IUserInputProvider interface that returns "y" when we ask it to GetUserInput.

Implementation
The concrete implementation we're going to be using will use the console. Nothing prevents making another concrete implementation that pops up a dialog window instead - as long as we use a prompt and return a string, anything can work. This means the implementation(s) can be modified in every possible way, the only assumption the MyApplication class makes is that there's a GetUserInput method that takes a string prompt and returns a string.
This could be an implementation:
public class ConsoleUserInputProvider : IUserInputProvider
{
    public string GetUserInput(string prompt)
    {
        Console.WriteLine(prompt);
        return Console.ReadLine();
    }

    public T GetUserInput<T>(string prompt, IUserInputValidator<T> validator)
    {
        string input;
        T result;

        var isValidInput = false;
        while(!isValidInput)
        {
            input = GetUserInput(prompt);
            isValidInput = validator.Validate(input, out result);
        }

        return result;
    }
}

Where IUserInputValidator is yet another abstraction that exposes a bool Validate(string input) method.
Let's make it a generic interface:
public interface IUserInputValidator<T>
{
    bool Validate(string input, out T result);
}

One could implement it like this:
public class BirthDateValidator : IUserInputValidator<DateTime>
{
    public bool Validate(string input, out DateTime result)
    {
        return DateTime.TryParse(input, out result);
    }
}

Or like this:
public enum YesNoResult
{
    Unknown,
    Yes,
    No
}

public class YesNoValidator : IUserInputValidator<YesNoResult>
{
    private readonly IDictionary<string, YesNoResult> _values;

    public YesNoValidator(IDictionary<string, YesNoResult> values)
    {
        _values = values;
    }

    public bool Validate(string input, out YesNoResult result)
    {
        if (string.IsNullOrEmpty(input))
        {
            throw new ArgumentException("input", "input string is empty.");
        }

        var lowerCase = input.Substring(0, 1).ToLower();

        var isValue = values.TryGetValue(lowerCase, out result);
        if (!isValue)
        {
            result = YesNoResult.Unknown;
        }

        return (result != YesNoResult.Unknown);
    }
}

As you can see, this approach produces very focused and specialized code - code that does one thing, so well, that it can't possibly even need to change. And yet it remains extensible - you could decorate any Validator implementation with, say, a ValidationLoggerDecorator that can log all failed validations:
public class ValidationLoggerDecorator<T> : IUserInputValidator<T>
{
    private readonly IUserInputValidator<T> _validator;
    private readonly ILogger _logger;

    public ValidationLoggerDecorator(IUserInputValidator<T> validator, ILogger logger)
    {
        _validator = validator;
        _logger = logger;
    }

    public bool Validate(string input, out T result)
    {
        var isValid = _validator.Validate(input, out result);

        if (!isValid)
        {
            _logger.Info(string.Format("Validation failed for input '{0}'.", input));
        }

        return isValid;
    }
}

Let's step back and take a look at what we've got here:

The code remains focused on a single responsibility - following the Single Responsibility Principle.

The extensibility depicted with the decorator example, is a side-effect of the Open/Closed principle: a class is closed for modification, open for extension.

The fact that the MyApplication class can work with any implementation of the IUserInputProvider interface, regardless of what dependencies that implementation might have, is a side-effect of the Liskov Substitution Principle.

Following the Interface Segregation Principle makes our interfaces be very focused as well, ideally exposing only a single member. This point greatly influences cohesion.

The fact that all implementations ("concrete" classes) depend on abstractions, and that these dependencies are injected into their constructor, is following the Dependency Inversion Principle*.

Together, these 5 points spell SOLID.

The IUserInputValidator we're passing to the GetUserInput() method, must come from somewhere. But if we create a new BirthDateValidator() our class will be tightly coupled with that specific implementation, and it will become very hard to test the GetUserInput() method and control validation from the outside.
The MyApplication class can receive the validators it needs in its constructor, and we can extract some of the logic from the Run method, into their own private methods:
public class MyApplication
{
    private readonly IUserInputProvider _inputProvider;
    private readonly IUserInputValidator<DateTime> _dateValidator;
    private readonly IUserInputValidator<YesNoResult> _confirmationValidator;

    public MyApplication(IUserInputProvider inputProvider,
                         IUserInputValidator<DateTime> dateValidator,
                         IUserInputValidator<YesNoResult> confirmationValidator)
    {
        _inputProvider = inputProvider;
        _dateValidator = dateValidator;
        _confirmationValidator = confirmationValidator;
    }

    public void Run()
    {
        var keepRunning = true;

        while(keepRunning)
        {
            var date = GetBirthDate();
            // ...

            keepRunning = !GetExitConfirmation();
        }
    }

    private DateTime GetBirthDate()
    {
        var prompt = "Enter your birth date:";
        var input = _inputProvider.GetUserInput(prompt, _dateValidator);

        return DateTime.Parse(input);
    }

    private bool GetExitConfirmation()
    {
        var prompt = "Exit (Y|N)?";
        var input = _inputProvider.GetUserInput(confirmPrompt, _confirmationValidator);

        return input == YesNoResult.Yes;
    }
}

Notice how the constructor could easily get bloated with possibly as many validators as there are things we want to get from the user. 3 constructor parameters is probably ok. More than that though, and I would be tempted to extract the validators into their own object, so as to keep the message clear: the MyApplication class needs validators - it doesn't do validation.
Let's extract them anyway to see what we get:
public class UserInputValidation
{
    private readonly IUserInputValidator<DateTime> _dateValidator;
    private readonly IUserInputValidator<YesNoResult> _confirmationValidator;

    public UserInputValidation(IUserInputValidator<DateTime> dateValidator,
                               IUserInputValidator<YesNoResult> confirmationValidator)
    {
        _dateValidator = dateValidator;
        _confirmationValidator = confirmationValidator
    }

    public IUserInputValidator<DateTime> DateValidator { get { return _dateValidator; } }
    public IUserInputValidator<YesNoResult> ConfirmationValidator { get { return _confirmationValidator; } }
}

Because the actual calculation algorithm would, by itself, be a reason to change, it's best to encapsulate it in its own class.
@svick's answer could be an implementation of some IAgeCalculator interface.
The MyApplication class now looks like this:
public class MyApplication
{
    private readonly IUserInputProvider _inputProvider;
    private readonly IAgeCalculator _calculator;
    private readonly UserInputValidation _validation;

    public MyApplication(IUserInputProvider inputProvider,
                         IAgeCalculator calculator,
                         UserInputValidation validation)
    {
        _inputProvider = inputProvider;
        _calculator = calculator;
        _validation = validation;
    }

    public void Run()
    {
        var keepRunning = true;

        while(keepRunning)
        {
            var date = GetBirthDate();
            DisplayAge(date);

            keepRunning = !GetExitConfirmation();
        }
    }

    private DateTime GetBirthDate()
    {
        var prompt = "Enter your birth date:";
        var input = _inputProvider.GetUserInput(prompt, _validation.DateValidator);

        return DateTime.Parse(input);
    }

    private void DisplayAge(DateTime date)
    {
        var result = _calculator.GetDifferenceInYearsAndDays(date, DateTime.Today);
        var message = string.Format("Your age: {0} years and {1} days", result.Item1, result.Item2);
        _inputProvider.ShowMessage(message);
    }

    private bool GetExitConfirmation()
    {
        var prompt = "Exit (Y|N)?";
        var input = _inputProvider.GetUserInput(confirmPrompt, _validation.ConfirmationValidator);

        return input == YesNoResult.Yes;
    }
}

The above assumes a ShowMessage method was added to the IUserInputProvider interface; this method is implemented like this:
public void ShowMessage(string message)
{
    Console.WriteLine(message);
}

Now, the Main method can serve its purpose: compose the application!
static void Main(string[] args)
{
    var input = new ConsoleInputProvider();
    var calculator = new AgeCalculator();
    var logger = LogManager.GetLogger("logger"); // gets a NLog logger
    var dateValidator = new ValidationLoggerDecorator(new BirthDateValidator(), logger);
    var yesNoValues = new Dictionary<string, YesNoResult>
                          {
                              { "y", YesNoResult.Yes },
                              { "n", YesNoResult.No }
                          };
    var confirmationValidator = new YesNoValidator(values);
    var validation = new UserInputValidation(dateValidator, confirmationValidator);

    var app = new MyApplication(input, calculator, validation);
    app.Run();
}

As you can see, inversion of control makes it easy to change our minds and swap the dateValidator for a simple BirthDateValidator, or to write a unit test that will only test how date validation operates, or only how age gets calculated, independently of everything else, without requiring user intervention, and with every dependency under full control.
Of course the composition root for this trivial application is instantiating the objects manually (a.k.a. poor man's DI), and it's quite manageable. For a bigger application, you could leave this daunting task to your favorite IoC container, leaving your Main method looking something like this (here using Ninject):
static void Main(string[] args)
{
    var kernel = new StandardKernel(new MyApplicationNinjectModule());
    var app = kernel.Get<MyApplication>();
    app.Run();
}

The entire application's dependency graph gets resolved in a single method call - Ninject isn't the fastest at that, but its nice syntax and great extensibility make it a solid candidate to consider. If you don't know what a StandardKernel does, the above code might seem automagical if I tell you that it does the exact same thing as the previous snippet.
And that's long enough.


Function to create URL relative to a configured base URL I have written this function to create a URL:

function createUrl(urlPath) {
    const endpoint = url.parse(env.config.get('myEndpoint'));
    endpoint.pathname = path.posix.join(endpoint.pathname, urlPath);
    return endpoint;
}

const basically creates a mutable object whose properties can still be modified.
Here, I need to declare some Object which will only be modified once. Am I correct in declaring endpoint as const?

Yes, it's correct to use const here. That would be considered idiomatic Javascript. If you're using const when you shouldn't, you'll notice as it will produce a TypeError.

People coming from other languages might disagree as they think that const means that the variable is constant/immutable (which is not the case, it's just not re-assignable). But that is really a critique of the naming of the keyword, not your usage of the keyword (which is correct).


Transforming a list of two-dimensional coordinates into a flat list of relative changes I have a little function that parse a nested list of coordinates into a flat list of compressed coordinates. By compressed coordinates, I mean that only the delta (distance) between each coordinates are stored in the list and the float coordinates are transformed in integer.

input = [[-8081441,5685214], [-8081446,5685216], [-8081442,5685219], [-8081440,5685211], [-8081441,5685214]]
output = [-8081441, 5685214, 5, -2, -4, -3, -2, 8, 1, -3]

def parseCoords(coords):
    #keep the first x,y coordinates
    parsed = [int(coords[0][0]), int(coords[0][1])]
    for i in xrange(1, len(coords)):
        parsed.extend([int(coords[i-1][0]) - int(coords[i][0]), int(coords[i-1][1]) - int(coords[i][1])])
    return parsed

parsedCoords = parseCoords(input)

As the input list is really big, is there a way to improve the function, maybe by using generators or list comprehension?

for i in xrange(1, len(coords)) is a red flag, since it is preferred in python to iterate directly over the elements rather than the indices. If you trully need the indices, you can still use enumerate.

Here it would look like

for i, coord in enumerate(coords[1:]):
    previous = coords[i]
    parsed.extend([int(previous[0] - coord[0]), int(previous[1] - coord[1])])

which seems worse as it

creates a copy of coords when slicing it;
still uses an index to retrieve the previous element.

Instead, it seems better to convert the list into an iterator an manually handle the current/previous coordinates. Something like:

def parse_coordinates(coords):
    iterator = iter(coords)
    previous_x, previous_y = iterator.next()
    parsed = [int(previous_x), int(previous_y)]
    for current_x, current_y in iterator:
        parsed.append(int(previous_x - current_x))
        parsed.append(int(previous_y - current_y))
        previous_x, previous_y = current_x, current_y
    return parsed

You can note the use of append instead of extend that will avoid building a temporary list. append try to be smart when resizing the underlying array so that two consecutive appends should not have more performance hit than extend.

But all in all, using append or extend in a for loop is often better written using a list-comprehension or a generator. You can easily turn this function into a generator by turning these append into yields:

def parse_coordinates(coords):
    iterator = iter(coords)
    previous_x, previous_y = iterator.next()
    yield int(previous_x)
    yield int(previous_y)
    for current_x, current_y in iterator:
        yield int(previous_x - current_x)
        yield int(previous_y - current_y)
        previous_x, previous_y = current_x, current_y

There is an other advantage to this approach: if the coords parameter is empty, your approach using coords[0] and the first one building a list using iterator.next() will crash raising either an IndexError or a StopIteration.

This generator can easily be fed to the list constructor or a for loop and won't crash; producing either an empty list or not entering the loop.

Lastly, you could drop manually managing the previous/current thing by using itertools.tee which is the key feature of the pairwise recipe:

from itertools import tee, izip

def parse_coordinates(coords):
    prev, cur = tee(coords)
    x, y = cur.next()
    yield int(x)
    yield int(y)
    for (previous_x, previous_y), (current_x, current_y) in izip(prev, cur):
        yield int(previous_x - current_x)
        yield int(previous_y - current_y)


Remove control and non-ASCII characters from large files I get huge incoming files (up to 6GB) and they are littered with Control and non-ASCII characters.  I need to strip them and I made this routine (below).  The problem is that it is insanely slow.  I would love any thoughts or advice on how I can speed it up.

public void StripHighBitCharacters(string fn)
{
    string writeFile = fn + "B";
    using (var reader = new StreamReader(fn))
    using (var writer = new StreamWriter(writeFile))
    {
        while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();
            if (line.Length > 0)
            {
                writer.WriteLine(BuildClearString(line));
            }
            else
            {
                writer.WriteLine(line);
            }
        }
    }
    File.Copy(writeFile, fn, true);
    File.Delete(writeFile);
}
public string BuildClearString(string line)
{

    StringBuilder sb = new StringBuilder();

    foreach (char c in line)
    {
        if (c >= 32 && c <= 175)
        {
            sb.Append(c);
        }
    }

    return (sb.ToString());
}

One source of performance degradation is frequent memory allocations.  In your case, the StringBuilder will allocate space for every line in your file, and may allocate additional space (along with a data copy) for longer lines.

You can eliminate all of that by reusing the StringBuilder object.  At the start of BuildClearString, call the clear method on it (sb.Clear(); or sb.Length = 0;).  Follow that up by a capacity check.

if (sb.Capacity < line.Length)
    sb.Capacity = line.Length;

By changing the capacity you ensure that you have a buffer big enough to hold all the characters you will be adding, so you won't incur any memory allocations when processing a line.  By reusing it, you keep the existing allocated memory, thus avoiding any allocations for later lines unless the line is longer than any you've already encountered.  You can also set an initial capacity on the StringBuilder object.


Cutting substrings in Haskell How can I improve readability of this code? indentChar needs to be reused elsewhere.

toCoordinate = dropWhile ((||) <$> (not . indentChar) <*> (=='(')) >>> takeWhile(/= ' ')
indentChar = (||) <$> isAlpha <*> (=='(')

Desired result:

*Parse> toCoordinate " \\--(my.coordinate - from outerspace)"
"my.coordinate"

The first thing I observe is that the dropWhile function argument is:

\x -> (not . indentChar) x || '(' == x

The comparison to '(' cancels out that same comparison in indentChar -- so the following definition of toCoordinate is identical in meaning:

toCoordinate = dropWhile (not . isAlpha) >>> takeWhile (/= ' ')

You did state that you need to use indentChar elsewhere -- but not using it here makes your code clearer.

Additionally, rather than use arrows, the above definition seems clearer to me when using function composition -- I would write it as:

toCoordinate = takeWhile (/= ' ') . dropWhile (not . isAlpha)