问:
Don't highlight HTML areas on editing CSS rules in Chrome
On the latest Chrome version, we got some powerful CSS tools to create rules using buttons and a highlight on every affected area when editing any rule in the inspector, this is very useful, but also some annoying to design a website.

If I am editing a rule like padding or margin, it shows a green or orange area around the element, for me is hard to say where is the border exactly with this colored area, I prefer to see the "REAL" result.

On the following image I am editing the padding and I can not see the border.

If I want to edit the border color to a different color, the line color turn into orange, then I can not see the color I am changing.

On the following image I am changing the border color to gray, as you can see it shows an orange border instead.

How do I disable/turn off this feature?

Google Chrome Version 73.0.3683.86

答:
Test Text

This is what i could find for you, it might just work for you.
Full post here: How to stop highlighting of a div element when double-clicking

Body:
<div unselectable="on">Test Text</div>

This is what i could find for you, it might just work for you.
Full post here: How to stop highlighting of a div element when double-clicking


来源: stackexchange

问:
Are there any open source C libraries with common data structures?
I'm looking for a C library with common reusable data structures like linked lists, hash tables etc. Something like the source distributed with Mastering Algorithms with C (Paperback) by Kyle Loudon.</p>

答: CLIB</p>

OwnerDisplayName: Vasil

ContentLicense: CC BY-SA 2.5

LastEditorDisplayName: Vasil

Body: CLIB

ContentLicense: CC BY-SA 3.0


问:
Regex for accepting greek characters with tone char(')
i'm trying to find a regex for allowing only greek characters to pass. But as you know greek language supports an emphasis tote for the most worlds:

For example the world χρήμα which means money has ' character above η
How to detect this char with a regex? My current regex is this:

```<code>/^[\sΑ-Ω0-9α-ω!$@#*+=._%,-/]*$/
</code>```

答:
You could use a character class such as

```<code>[\u0370-\u03ff\u1f00-\u1fff]+
</code>```

See a demo on regex101.com.

ContentLicense: CC BY-SA 4.0

Body:
i'm trying to find a regex for allowing only greek characters to pass. But as you know greek language supports an emphasis tote for the most worlds:

For example the world χρήμα which means money has ' character above η
How to detect this char with a regex? My current regex is this:

/^[\sΑ-Ω0-9α-ω!$@#*+=._%,-/]*$/

Body:
You could use a character class such as

[\u0370-\u03ff\u1f00-\u1fff]+

See a demo on regex101.com.


问:
How can I check what version/edition of Visual Studio is installed programmatically?
I could read registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0.
However, it doesn't give me any information about the edition of it - Professional/Ultimate or whatever.

How can I get the information with programmatically (preferably python)?

答:
Run the path in cmd
C:\Program Files (x86)\Microsoft Visual Studio\Installer>vswhere.exe</p>


问:
Simplifying compound regular expressions using python regex
I have a list of compound and simple regular expressions that I need to simplify. Lets say ["(crazy|cute|funny)( \w+){0,5} cat", "I love( \w+){0,5} cats)"]. I need to identify the ones which have a specific pattern. In example, I want to identify the ones twhich contains or("|")  and simplify them to a single pattern.

In the above example I want to convert "(crazy|cute|funny)( \w+){0,5} cat to ["crazy( \w+){0,5} cat", "cute( \w+){0,5} cat", "funny( \w+){0,5} cat"]. I do not want to change "I love( \w+){0,5} cats)" cause it's a simple and not compound Regex.

I have came up with the following regex to identify such examples:

```<code>re = "\((\w+{1}\|){1,10}\w+{1}\)"
</code>```

But it does not identify them. I am not sure what is wrong.

```<code>def sep_rules(search_list):
    new_query_list = list()
    for item in search_list:
        if "|" not in item:
            new_query_list.append(item)
        else:
            re = "\((\w+{1}\|){1,10}\w+{1}\)"
            print item
            print re_search(item, re) # does not identify them correctly
</code>```

Input data:

```<code>["(crazy|cute|funny)( \w+){0,5} cat", "I love( \w+){0,5} cats)"]
</code>```

Expected output:

```<code>["crazy( \w+){0,5} cat" , "cute( \w+){0,5} cat" , "funny( \w+){0,5} cat" , "I love( \w+){0,5} cats)"]
</code>```

答:
The error I get when compiling this pattern is "multiple repeat", referring to the pattern \w+{1}. This pattern indeed doesn't really make sense - you're basically applying two repeat directions (+, at least once, and {1}, exactly once) to the same expression \w.

If you wanted, you could put parentheses around it as in (\w+){1}, but you should be aware that having {1} anywhere in a regex is redundant in the first place; <expression>{1} is exactly equivalent to <expression>.

(Note: More generally, the problem you're trying to solve - minimization of regular expressions - is at least NP-hard, and certainly not solvable with another regular expression. You can at best apply a few very limited special cases.)

Body:
I have a list of compound and simple regular expressions that I need to simplify. Lets say ["(crazy|cute|funny)( \w+){0,5} cat", "I love( \w+){0,5} cats)"]. I need to identify the ones which have a specific pattern. In example, I want to identify the ones twhich contains or("|")  and simplify them to a single pattern.

In the above example I want to convert "(crazy|cute|funny)( \w+){0,5} cat to ["crazy( \w+){0,5} cat", "cute( \w+){0,5} cat", "funny( \w+){0,5} cat"]. I do not want to change "I love( \w+){0,5} cats)" cause it's a simple and not compound Regex.

I have came up with the following regex to identify such examples:

re = "\((\w+{1}\|){1,10}\w+{1}\)"

But it does not identify them. I am not sure what is wrong.

def sep_rules(search_list):
    new_query_list = list()
    for item in search_list:
        if "|" not in item:
            new_query_list.append(item)
        else:
            re = "\((\w+{1}\|){1,10}\w+{1}\)"
            print item
            print re_search(item, re) # does not identify them correctly

Input data:

["(crazy|cute|funny)( \w+){0,5} cat", "I love( \w+){0,5} cats)"]

Expected output:

["crazy( \w+){0,5} cat" , "cute( \w+){0,5} cat" , "funny( \w+){0,5} cat" , "I love( \w+){0,5} cats)"]


问:
Using "Either" in data validation
I have a program which is dedicated to create "Student" (First Name, Last Name, Age) and validate input data. My problem is that: when I insert student which e.g either name hasn't 2 letters or age is lower than 18 - the program only show one error. How to use "Either function" to create for example a string which contains all errors ? </p>

```<code>module Student where

data Student = Student {firstName::FirstName, lastName::LastName, age::Age}
  deriving Show

newtype FirstName = FirstName String
  deriving Show

newtype LastName = LastName String
  deriving Show

newtype Age = Age Int
  deriving Show

mkStudent :: String -> String -> String -> Either String Student
mkStudent fn ln a =
   Student <$> validate fn
           <*> validate ln
           <*> validate a

aceptableLetters = ['a'..'z']++['A'..'Z']

validateFn :: String -> Either String FirstName
validateFn fn
   | length fn < 2 = Left "First name has to at least 2 letters"
   | length fn > 100 = Left "First name is limited to 100 characters"
   | not . null $ filter (\c -> not . elem c $ aceptableLetters) fn = Left "First name contains unacceptable chars"
   | otherwise = Right . FirstName $ fn

validateLn :: String -> Either String LastName
validateLn lastName
   | length lastName < 2 = Left "Last name has to at least 2 letters"
   | length lastName > 100 = Left "Last name is limited to 100 characters"
   | not . null $ filter (\c -> not . elem c $ aceptableLetters) lastName = Left "Last name contains unacceptable chars"
   | otherwise = Right . LastName $ lastName

validateA :: String -> Either String Age
validateA a
   | age <= 18 = Left "Student has to be at least 18"
   | age > 100 = Left "Student has more than 100 years. Probably it is an error."
   | otherwise = Right . Age $ age
   where
    age = read a

class Validate v where
    validate :: String -> Either String v

instance Validate FirstName where
    validate=validateFn

instance Validate LastName where
    validate=validateLn

instance Validate Age where
    validate=validateA
</code>```

答:
A basic way would be to define a custom helper function which collects the errors in a list:

```<code>collectError :: Error e a -> [e]
collectError (Left e) = [e]
collectError _        = []   -- no errors
</code>```

Then, we can exploit the helper as follows:

```<code>mkStudent :: String -> String -> String -> Either [String] Student
mkStudent fn ln a = case (validate fn, validate ln, validate a) of
   (Right xfn, Right xln, Right xa) -> Right (Student xfn xln xa)
   (efn      , eln      , ea      ) ->
      Left (collectError efn ++ collectError eln ++ collectError ea)
</code>```

This is probably not the most elegant way, but it is simple.

If this pattern is frequently used in a program, I'd be tempted to craft a custom Applicative which logs all the errors. Something like

```<code>data Result e a = Error e | OK a
  deriving Functor

instance Semigroup e => Applicative (Result e) where
   pure = OK
   (OK f) <*> (OK x) = OK $ f x
   (Error e1) <*> (Error e2) = Error (e1 <> e2)
   (Error e1) <*> _          = Error e1
   _          <*> (Error e2) = Error e2
</code>```

(This should already exist in the libraries under some name.)

Then,

```<code>mkStudent :: String -> String -> String -> Result [String] Student
mkStudent fn ln a =
   Student <$> validate fn
           <*> validate ln
           <*> validate a
</code>```

should just work, provided the validation function return Error ["message"] on failure, with return type Result [String] T.

Title: Using "Either" in data validation

Body:
I have a program which is dedicated to create "Student" (First Name, Last Name, Age) and validate input data. My problem is that: when I insert student which e.g either name hasn't 2 letters or age is lower than 18 - the program only show one error. How to use "Either function" to create for example a string which contains all errors ?

module Student where

data Student = Student {firstName::FirstName, lastName::LastName, age::Age}
  deriving Show

newtype FirstName = FirstName String
  deriving Show

newtype LastName = LastName String
  deriving Show

newtype Age = Age Int
  deriving Show

mkStudent :: String -> String -> String -> Either String Student
mkStudent fn ln a =
   Student <$> validate fn
           <*> validate ln
           <*> validate a

aceptableLetters = ['a'..'z']++['A'..'Z']

validateFn :: String -> Either String FirstName
validateFn fn
   | length fn < 2 = Left "First name has to at least 2 letters"
   | length fn > 100 = Left "First name is limited to 100 characters"
   | not . null $ filter (\c -> not . elem c $ aceptableLetters) fn = Left "First name contains unacceptable chars"
   | otherwise = Right . FirstName $ fn

validateLn :: String -> Either String LastName
validateLn lastName
   | length lastName < 2 = Left "Last name has to at least 2 letters"
   | length lastName > 100 = Left "Last name is limited to 100 characters"
   | not . null $ filter (\c -> not . elem c $ aceptableLetters) lastName = Left "Last name contains unacceptable chars"
   | otherwise = Right . LastName $ lastName

validateA :: String -> Either String Age
validateA a
   | age <= 18 = Left "Student has to be at least 18"
   | age > 100 = Left "Student has more than 100 years. Probably it is an error."
   | otherwise = Right . Age $ age
   where
    age = read a

class Validate v where
    validate :: String -> Either String v

instance Validate FirstName where
    validate=validateFn

instance Validate LastName where
    validate=validateLn

instance Validate Age where
    validate=validateA

Body:
A basic way would be to define a custom helper function which collects the errors in a list:

collectError :: Error e a -> [e]
collectError (Left e) = [e]
collectError _        = []   -- no errors

Then, we can exploit the helper as follows:

mkStudent :: String -> String -> String -> Either [String] Student
mkStudent fn ln a = case (validate fn, validate ln, validate a) of
   (Right xfn, Right xln, Right xa) -> Right (Student xfn xln xa)
   (efn      , eln      , ea      ) ->
      Left (collectError efn ++ collectError eln ++ collectError ea)

This is probably not the most elegant way, but it is simple.

If this pattern is frequently used in a program, I'd be tempted to craft a custom Applicative which logs all the errors. Something like

data Result e a = Error e | OK a
  deriving Functor

instance Semigroup e => Applicative (Result e) where
   pure = OK
   (OK f) <*> (OK x) = OK $ f x
   (Error e1) <*> (Error e2) = Error (e1 <> e2)
   (Error e1) <*> _          = Error e1
   _          <*> (Error e2) = Error e2

(This should already exist in the libraries under some name.)

Then,

mkStudent :: String -> String -> String -> Result [String] Student
mkStudent fn ln a =
   Student <$> validate fn
           <*> validate ln
           <*> validate a

should just work, provided the validation function return Error ["message"] on failure, with return type Result [String] T.


问:
String to float issues with absolute value
My program solves quadratic equations and finds the amount of real solutions. I was trying to tune up my program by cleaning things up such as, if the input of a,b or c is either 1 or -1, display the function as x^2+x+c (if user puts in one for all) or -x^2-x-c (if user picks -1 for all of them.) This should be the case for all positive or negative numbers.
So in reality, it should put out if a is 6, b is -7, and c is 4, it should output as 6x^2-7x+4, but currently it is 6x^2+-2x+4. I know something has to  be done with the absolute value function, but just trying to fix the -1 and 1 situation gives me errors with converting string to float.
The error I get is
Traceback (most recent call last):
  File "C:/Users/danco/PycharmProjects/ECE 122/a_hermon_ECE122project1.py", line 26, in
    x = float(x)
ValueError: could not convert string to float:

I tried using the absolute value function, but the errors with the float to string before using it are still carrying over. This is all in the evaluate_quad_function.

```<code>a = 0
b = 0
c = 0
import math
def evaluate_quad_function(a, b, c, x):
    f_of_x = a*(x**2) + (b * x) + c
    return f_of_x  # return the calculated result
print("Welcome to the Quadratic Solver for f(x)=ax^2+bx+c")
a = input("Enter Value for a:")
b = input("Enter Value for b:")
c = input("Enter Value for c:")
# cannot do math on string, convert to float
a = float(a)
b = float(b)
c = float(c)
f_x0= a*((-b)/(2*a)**2) + (b * (-b)/(2*a)) + c

if a == 1 and b == 1:
    print("\nFunction is: f(x)= ","x**2 +","x +", c, sep="")

if a == -1 and b == -1:
    print("\nFunction is: f(x)= ","-x**2 -","x +", c, sep="")
else:
    print("\nFunction is: f(x)= ", a, "x**2 +", b, "x +", c, sep="")
x = input("Enter Value for x:")
x = float(x)

fx = evaluate_quad_function(a, b, c, x)
print("F"+"("+str(x)+"=", str(fx))
temp = input("\nPress Enter to continue...")  # wait

x0 = -b/(2*a)
x0 = float(x0)
if a>0:
    print("f(x) has a minimum at "+ str(x0)+" with a value f(x0)="+ str(f_x0))
else:
    print("f(x) has a maximum at " + str(x0) + " with a value f(x0)=" + str(f_x0))
temp = input("\nPress Enter to continue...")  # wait

def compute_discriminate():
    print("Solving for f(x) = 0")
    d = (b**2) - (4*a*c)
    x1 = (-b + math.sqrt(d))/(2*a)
    x2 = (-b - math.sqrt(d))/(2*a)
    print("Discriminant is", str(d))
    if d > 0:
        (print("Two Real Solutions",str(x1), "and", str(x2) ))
    if d == 0:
        print("One real solution:", str(x0))
    else:
        print("No real solution.")
print("\nThanks for using Quadratic Solver!, come back soon.")
print(compute_discriminate())
</code>```

答:
You can just create a function to print + or - before a number.

Try this:

```<code>def print_sign(x):
    # check the sign if positive or negative
    return '+' if x > 0 else '-'

print("\nFunction is: f(x)= ", a, "x**2 ",print_sign(b), abs(b), "x ", print_sign(c), abs(c), sep="")
</code>```

Result:
Function is: f(x)= 1.0x**2 -4.0x +3.0

====EDIT====
Replace these lines with the code above

```<code>if a == 1 and b == 1:
    print("\nFunction is: f(x)= ","x**2 +","x +", c, sep="")

if a == -1 and b == -1:
    print("\nFunction is: f(x)= ","-x**2 -","x +", c, sep="")
else:
    print("\nFunction is: f(x)= ", a, "x**2 +", b, "x +", c, sep="")
</code>```

Body:
My program solves quadratic equations and finds the amount of real solutions. I was trying to tune up my program by cleaning things up such as, if the input of a,b or c is either 1 or -1, display the function as x^2+x+c (if user puts in one for all) or -x^2-x-c (if user picks -1 for all of them.) This should be the case for all positive or negative numbers.
So in reality, it should put out if a is 6, b is -7, and c is 4, it should output as 6x^2-7x+4, but currently it is 6x^2+-2x+4. I know something has to  be done with the absolute value function, but just trying to fix the -1 and 1 situation gives me errors with converting string to float.
The error I get is
Traceback (most recent call last):
  File "C:/Users/danco/PycharmProjects/ECE 122/a_hermon_ECE122project1.py", line 26, in
    x = float(x)
ValueError: could not convert string to float:

I tried using the absolute value function, but the errors with the float to string before using it are still carrying over. This is all in the evaluate_quad_function.

a = 0
b = 0
c = 0
import math
def evaluate_quad_function(a, b, c, x):
    f_of_x = a*(x**2) + (b * x) + c
    return f_of_x  # return the calculated result
print("Welcome to the Quadratic Solver for f(x)=ax^2+bx+c")
a = input("Enter Value for a:")
b = input("Enter Value for b:")
c = input("Enter Value for c:")
# cannot do math on string, convert to float
a = float(a)
b = float(b)
c = float(c)
f_x0= a*((-b)/(2*a)**2) + (b * (-b)/(2*a)) + c

if a == 1 and b == 1:
    print("\nFunction is: f(x)= ","x**2 +","x +", c, sep="")

if a == -1 and b == -1:
    print("\nFunction is: f(x)= ","-x**2 -","x +", c, sep="")
else:
    print("\nFunction is: f(x)= ", a, "x**2 +", b, "x +", c, sep="")
x = input("Enter Value for x:")
x = float(x)

fx = evaluate_quad_function(a, b, c, x)
print("F"+"("+str(x)+"=", str(fx))
temp = input("\nPress Enter to continue...")  # wait

x0 = -b/(2*a)
x0 = float(x0)
if a>0:
    print("f(x) has a minimum at "+ str(x0)+" with a value f(x0)="+ str(f_x0))
else:
    print("f(x) has a maximum at " + str(x0) + " with a value f(x0)=" + str(f_x0))
temp = input("\nPress Enter to continue...")  # wait

def compute_discriminate():
    print("Solving for f(x) = 0")
    d = (b**2) - (4*a*c)
    x1 = (-b + math.sqrt(d))/(2*a)
    x2 = (-b - math.sqrt(d))/(2*a)
    print("Discriminant is", str(d))
    if d > 0:
        (print("Two Real Solutions",str(x1), "and", str(x2) ))
    if d == 0:
        print("One real solution:", str(x0))
    else:
        print("No real solution.")
print("\nThanks for using Quadratic Solver!, come back soon.")
print(compute_discriminate())

Body:
You can just create a function to print + or - before a number.

Try this:

def print_sign(x):
    # check the sign if positive or negative
    return '+' if x > 0 else '-'

print("\nFunction is: f(x)= ", a, "x**2 ",print_sign(b), abs(b), "x ", print_sign(c), abs(c), sep="")

Result:
Function is: f(x)= 1.0x**2 -4.0x +3.0

====EDIT====
Replace these lines with the code above

if a == 1 and b == 1:
    print("\nFunction is: f(x)= ","x**2 +","x +", c, sep="")

if a == -1 and b == -1:
    print("\nFunction is: f(x)= ","-x**2 -","x +", c, sep="")
else:
    print("\nFunction is: f(x)= ", a, "x**2 +", b, "x +", c, sep="")


问:
Sub-Route Remote Call with Tink Web
I defined a sub-route and i want to call it from my JsClient.

I tried to follow the Quick Start instructions but sub-route call isn't documented.

```<code>class App {

    public static function main() {
        var remote = new Remote<ApiRoute>(new JsClient(), new RemoteEndpoint(new Host("localhost",8080)));
        remote.api.upload().handle(function(o) switch o {
            case Success(result): trace(result);
            case Failure(e): trace(e);
        });
    }

}

class ApiRoute {
    public function new() { }

    @:sub public var api = new Api();
}

class Api {

    public function new() { }

    @:post('/upload')
    public function upload(){
        return '';
    }

}

</code>```

The compiler doesn't like it :
Void -> tink.web.proxy.Remote1 has no field upload

答: It is not about sub routes. But vars/props are converted to function call in proxies. So you should write remote.api().upload()

Title: Sub-Route Remote Call with Tink Web

Body:
I defined a sub-route and i want to call it from my JsClient.

I tried to follow the Quick Start instructions but sub-route call isn't documented.

class App {

    public static function main() {
        var remote = new Remote<ApiRoute>(new JsClient(), new RemoteEndpoint(new Host("localhost",8080)));
        remote.api.upload().handle(function(o) switch o {
            case Success(result): trace(result);
            case Failure(e): trace(e);
        });
    }

}

class ApiRoute {
    public function new() { }

    @:sub public var api = new Api();
}

class Api {

    public function new() { }

    @:post('/upload')
    public function upload(){
        return '';
    }

}

The compiler doesn't like it :
Void -> tink.web.proxy.Remote1 has no field upload


问:
What does the syntax [] mean in this case?
I'm learning how to use shader in pyopengl through a sample project on GitHubGist. I came across "[]" syntax and I don't know what it means in this case.

I only know one use of "[]" which is indexing an array.

    def initShader(self, vertex_shader_source, fragment_shader_source):
        # create program
        self.program=glCreateProgram()
        print('create program')
        printOpenGLError()

        # vertex shader
        print('compile vertex shader...')
        self.vs = glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(self.vs, [vertex_shader_source])
        glCompileShader(self.vs)
        glAttachShader(self.program, self.vs)
        printOpenGLError()
```

I don't understand the "[]" syntax used in glShaderSource()

答:
The function glShaderSource accepts a list of code snippets.

See PyOpenGL documentation of glShaderSource:

```<code>glShaderSource( GLhandle(shaderObj),[bytes(string),...]) -> None
</code>```

[vertex_shader_source] is the list of strings, where each string contains glsl source code ([] generates a List).

In this case the list contains only one element.
The elements of the list are concatenated and compiled.

See also the C specification of glShaderSource

Body:
I'm learning how to use shader in pyopengl through a sample project on GitHubGist. I came across "[]" syntax and I don't know what it means in this case.

I only know one use of "[]" which is indexing an array.

    def initShader(self, vertex_shader_source, fragment_shader_source):
        # create program
        self.program=glCreateProgram()
        print('create program')
        printOpenGLError()

        # vertex shader
        print('compile vertex shader...')
        self.vs = glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(self.vs, [vertex_shader_source])
        glCompileShader(self.vs)
        glAttachShader(self.program, self.vs)
        printOpenGLError()

I don't understand the "[]" syntax used in glShaderSource()

Body:
The function glShaderSource accepts a list of code snippets.

See PyOpenGL documentation of glShaderSource:

glShaderSource( GLhandle(shaderObj),[bytes(string),...]) -> None

[vertex_shader_source] is the list of strings, where each string contains glsl source code ([] generates a List).

In this case the list contains only one element.
The elements of the list are concatenated and compiled.

See also the C specification of glShaderSource


问:
Multiple foreign key constraints on a single column in SQL Server 2008
I have two tables TableA and TableB. Now TableA has 2 columns: ID & Name, TableB has 3 columns: ID, Name, & TableAID. Now there is a foreign key constraint from TableA.ID = TableB.TableAID. I need to be able to allow for TableB.TableAID to either use values from TableA.ID or TableB.ID, hence multiple foreign key constraints on a single column and I need it to allow either or. Thanks!</p>

答:
That is a clear sign of a bad design. You should never need to do this and if you do then you need to rethink your design. </p>

<p>You cannot make a field be associated with two different FKs. </p>

Body:
That is a clear sign of a bad design. You should never need to do this and if you do then you need to rethink your design.

You cannot make a field be associated with two different FKs.


问:
Rebasing commit from one parent onto another
I have an existing repository that I'm working on, yet recently I've learned a lot about good practices and the git itself. I wanted to shake my history to my liking, especially some early commits.

Below is the fragment of the current history. I want to rebase the e5cb9b8 commit onto the 1a92e84 so I can have just one/two levels of depth in the history graph (visually, changing the parent of e5cb9b8). I've tried rebasing

```<code>git rebase -p --onto 1a92e84 e5cb9b8 master
</code>```

and cherrypicking into new branch created from 9ecbe00. Cherrypicking gets me flat history, rebase fails with the following message

error: commit ca230d8c048d22de6f219da8a22e70a773827c38 is a merge but no -m option was given.

fatal: cherry-pick failed

Could not pick ca230d8c048d22de6f219da8a22e70a773827c38

```<code>* | 45a0a21 - (7 weeks ago) #17 Updates README.md - Kamil Pacanek
|/
*   ca230d8 - (8 weeks ago) Merge pull request #2 from KamilPacanek/feat/rmb-support-removing-parts - Kamil Pacanek
|\
| * e5cb9b8 - (8 weeks ago) Adds support for removing parts on ReactorCells - KamilPacanek
* |   1a92e84 - (8 weeks ago) Merge pull request #1 from KamilPacanek/enable-gh-pages - Kamil Pacanek
|\ \
| |/
| * 81761ff - (8 weeks ago) Adds GH Pages support - KamilPacanek
|/
* 9ecbe00 - (8 weeks ago) Initial commit - KamilPacanek
</code>```

Expected:

```<code>* | 45a0a21' - (7 weeks ago) #17 Updates README.md - Kamil Pacanek
|/
*   ca230d8' - (8 weeks ago) Merge pull request #2 from KamilPacanek/feat/rmb-support-removing-parts - Kamil Pacanek
|\
| * e5cb9b8' - (8 weeks ago) Adds support for removing parts on ReactorCells - KamilPacanek
|/
*    1a92e84 - (8 weeks ago) Merge pull request #1 from KamilPacanek/enable-gh-pages - Kamil Pacanek
|\
| |
| * 81761ff - (8 weeks ago) Adds GH Pages support - KamilPacanek
|/
* 9ecbe00 - (8 weeks ago) Initial commit - KamilPacanek
</code>```

I've searched the StackOverflow for similar issues and it seems like no one described such modifications so deep into the history.

Solution

Ok, for future readers and myself I'm writing down the solution for my problem. @alfunx answer was a key to solve it - I've tracked down in other topic explanation of git rebase and git rebase --onto and started to experiment on my repository. Through trial and error, I've managed* to achieve the expected history graph by executing

```<code>git rebase -ir --onto 1a92e84 81761ff develop
</code>```

and replacing followig fragment of todo-list

```<code>label onto

# Branch KamilPacanek/feat/rmb-support-removing-parts
reset onto
pick e5cb9b8 Adds support for removing parts on ReactorCells
label KamilPacanek/feat/rmb-support-removing-parts

# Branch enh/add-uranium-cells
reset 9ecbe00 # Initial commit
merge -C 1a92e84 onto # Merge pull request #1 from KamilPacanek/enable-gh-pages
merge -C ca230d8 KamilPacanek/feat/rmb-support-removing-parts # Merge pull request #2 from KamilPacanek/feat/rmb-support-removing-parts
label branch-point
</code>```

with following (changed the reset position and removed one, redundant, merge commit):

```<code>label onto

# Branch KamilPacanek/feat/rmb-support-removing-parts
reset onto
pick e5cb9b8 Adds support for removing parts on ReactorCells
label KamilPacanek/feat/rmb-support-removing-parts

# Branch enh/add-uranium-cells
reset onto
merge -C ca230d8 KamilPacanek/feat/rmb-support-removing-parts # Merge pull request #2 from KamilPacanek/feat/rmb-support-removing-parts
label branch-point
</code>```

*)
Actually, there was more to do to achieve that state, but I don't want to obfuscate the solution with secondary issues. First git rebase failed with that message:

error: refusing to update ref with bad name 'refs/rewritten/Implement-durability-loss.'

hint: Could not execute the todo command

hint:     label Implement-durability-loss.

As you can see I have a period at the end of the commit message. After I solved that (with another rebase with reword command) I was able to move forward.

Also, it seems like git rebase --abort doesn't clear the refs that are created during git rebase -r.. so I had a mess in history. Solved that one via rm -rf .git/refs/rewritten. Another thing, I had a leftover .git/sequencer folder that was removable via git revert --quit.

答:
The base you chose for the rebase is wrong and should be 81761ff instead of e5cb9b8. I would suggest you to do an interactive rebase and use --rebase-merges instead of --preserve-merges. So the command should be:

```<code>git rebase -ir --onto 1a92e84 81761ff master
</code>```

Now, Git will probably produce one additional merge commit. To avoid that and to produce your desired outcome, you should adjust the todo-list to something like:

```<code>label onto
pick e5cb9b8 Adds support for removing parts on ReactorCells
label new
reset onto
merge -C ca230d8 new
pick 45a0a21 #17 Updates README.md
# Remaining commits...
</code>```

In the todo-list, label can be used to mark the current commit (HEAD), and reset can be used to set the HEAD to some commit/label. merge is obviously used to produce a merge commit, -C makes the merge use the same commit message as the original merge commit.

This is quite advanced and really not needed for most users and use cases, so please consider man git-rebase, section Rebase Merges (or the online version here) for more in depth information. The example there is actually quite similar to your situation.

Body:
I have an existing repository that I'm working on, yet recently I've learned a lot about good practices and the git itself. I wanted to shake my history to my liking, especially some early commits.

Below is the fragment of the current history. I want to rebase the e5cb9b8 commit onto the 1a92e84 so I can have just one/two levels of depth in the history graph (visually, changing the parent of e5cb9b8). I've tried rebasing

git rebase -p --onto 1a92e84 e5cb9b8 master

and cherrypicking into new branch created from 9ecbe00. Cherrypicking gets me flat history, rebase fails with the following message

error: commit ca230d8c048d22de6f219da8a22e70a773827c38 is a merge but no -m option was given.

fatal: cherry-pick failed

Could not pick ca230d8c048d22de6f219da8a22e70a773827c38

* | 45a0a21 - (7 weeks ago) #17 Updates README.md - Kamil Pacanek
|/
*   ca230d8 - (8 weeks ago) Merge pull request #2 from KamilPacanek/feat/rmb-support-removing-parts - Kamil Pacanek
|\
| * e5cb9b8 - (8 weeks ago) Adds support for removing parts on ReactorCells - KamilPacanek
* |   1a92e84 - (8 weeks ago) Merge pull request #1 from KamilPacanek/enable-gh-pages - Kamil Pacanek
|\ \
| |/
| * 81761ff - (8 weeks ago) Adds GH Pages support - KamilPacanek
|/
* 9ecbe00 - (8 weeks ago) Initial commit - KamilPacanek

Expected:

* | 45a0a21' - (7 weeks ago) #17 Updates README.md - Kamil Pacanek
|/
*   ca230d8' - (8 weeks ago) Merge pull request #2 from KamilPacanek/feat/rmb-support-removing-parts - Kamil Pacanek
|\
| * e5cb9b8' - (8 weeks ago) Adds support for removing parts on ReactorCells - KamilPacanek
|/
*    1a92e84 - (8 weeks ago) Merge pull request #1 from KamilPacanek/enable-gh-pages - Kamil Pacanek
|\
| |
| * 81761ff - (8 weeks ago) Adds GH Pages support - KamilPacanek
|/
* 9ecbe00 - (8 weeks ago) Initial commit - KamilPacanek

I've searched the StackOverflow for similar issues and it seems like no one described such modifications so deep into the history.

Solution

Ok, for future readers and myself I'm writing down the solution for my problem. @alfunx answer was a key to solve it - I've tracked down in other topic explanation of git rebase and git rebase --onto and started to experiment on my repository. Through trial and error, I've managed* to achieve the expected history graph by executing

git rebase -ir --onto 1a92e84 81761ff develop

and replacing followig fragment of todo-list

label onto

# Branch KamilPacanek/feat/rmb-support-removing-parts
reset onto
pick e5cb9b8 Adds support for removing parts on ReactorCells
label KamilPacanek/feat/rmb-support-removing-parts

# Branch enh/add-uranium-cells
reset 9ecbe00 # Initial commit
merge -C 1a92e84 onto # Merge pull request #1 from KamilPacanek/enable-gh-pages
merge -C ca230d8 KamilPacanek/feat/rmb-support-removing-parts # Merge pull request #2 from KamilPacanek/feat/rmb-support-removing-parts
label branch-point

with following (changed the reset position and removed one, redundant, merge commit):

label onto

# Branch KamilPacanek/feat/rmb-support-removing-parts
reset onto
pick e5cb9b8 Adds support for removing parts on ReactorCells
label KamilPacanek/feat/rmb-support-removing-parts

# Branch enh/add-uranium-cells
reset onto
merge -C ca230d8 KamilPacanek/feat/rmb-support-removing-parts # Merge pull request #2 from KamilPacanek/feat/rmb-support-removing-parts
label branch-point

*)
Actually, there was more to do to achieve that state, but I don't want to obfuscate the solution with secondary issues. First git rebase failed with that message:

error: refusing to update ref with bad name 'refs/rewritten/Implement-durability-loss.'

hint: Could not execute the todo command

hint:     label Implement-durability-loss.

As you can see I have a period at the end of the commit message. After I solved that (with another rebase with reword command) I was able to move forward.

Also, it seems like git rebase --abort doesn't clear the refs that are created during git rebase -r.. so I had a mess in history. Solved that one via rm -rf .git/refs/rewritten. Another thing, I had a leftover .git/sequencer folder that was removable via git revert --quit.

Body:
The base you chose for the rebase is wrong and should be 81761ff instead of e5cb9b8. I would suggest you to do an interactive rebase and use --rebase-merges instead of --preserve-merges. So the command should be:

git rebase -ir --onto 1a92e84 81761ff master

Now, Git will probably produce one additional merge commit. To avoid that and to produce your desired outcome, you should adjust the todo-list to something like:

label onto
pick e5cb9b8 Adds support for removing parts on ReactorCells
label new
reset onto
merge -C ca230d8 new
pick 45a0a21 #17 Updates README.md
# Remaining commits...

In the todo-list, label can be used to mark the current commit (HEAD), and reset can be used to set the HEAD to some commit/label. merge is obviously used to produce a merge commit, -C makes the merge use the same commit message as the original merge commit.

This is quite advanced and really not needed for most users and use cases, so please consider man git-rebase, section Rebase Merges (or the online version here) for more in depth information. The example there is actually quite similar to your situation.


问:
Duck typing with python 3.5 style type-annotations
Supposing I have a function with a signature like:

```<code>def foo(self, name:str, stream):
    pass
</code>```

I want to add an annotation to the "stream" argument so which means "you can have any object x as long as x.readline()->str".

So that means I could use any python file object as an argument here (since it has a readline method), but I could also provide an object which implements nothing but readline and it would be perfectly acceptable.

How could I rewrite this function definition so that I can annotate the 2nd argument?

答:
As ivanl notes, PEP 544 adds Protocols to support 'static duck typing'. This PEP was accepted recently and was added in Python 3.8. You can also try Protocols in Python 3.6 and 3.7 with Mypy using the typing-extensions package.

In your case, you would define a very simple Protocol SupportsReadline with a single method and use this in the annotation of your function arguments:

# Python 3.8+, for 3.6 & 3.7 replace 'typing' with 'typing_extensions'.
from typing import Protocol

class SupportsReadline(Protocol):
    def readline(self) -> str:
        ...

def func(name: str, stream: SupportsReadline) -> None:
    pass
```

<p>Now any object with a <code>readline</code> method with a compatible signature is an implicit subtype of <code>SupportsReadline</code> and satisfies the annotation of your function argument. Note that <code>LineRepeater</code> does not inherit explicitly from <code>SupportsReadline</code>:</p>

<pre class="lang-py prettyprint-override"><code>class LineRepeater:
    def readline(self) -> str:
        return "Hello again!"

func("a", LineRepeater())  # OK
</code>```

The same holds for other objects if the method signature matches exactly:

from io import BytesIO, StringIO

func("a", StringIO())  # OK
func("a", open("foo.txt"))  # OK
func("a", BytesIO())  # ERROR (return type is bytes instead of str)
func("a", [])  # ERROR
func("a", 1)  # ERROR
func("a", object())  # ERROR
```

Body:
Supposing I have a function with a signature like:

def foo(self, name:str, stream):
    pass

I want to add an annotation to the "stream" argument so which means "you can have any object x as long as x.readline()->str".

So that means I could use any python file object as an argument here (since it has a readline method), but I could also provide an object which implements nothing but readline and it would be perfectly acceptable.

How could I rewrite this function definition so that I can annotate the 2nd argument?

Body:
As ivanl notes, PEP 544 adds Protocols to support 'static duck typing'. This PEP was accepted recently and was added in Python 3.8. You can also try Protocols in Python 3.6 and 3.7 with Mypy using the typing-extensions package.

In your case, you would define a very simple Protocol SupportsReadline with a single method and use this in the annotation of your function arguments:

# Python 3.8+, for 3.6 & 3.7 replace 'typing' with 'typing_extensions'.
from typing import Protocol

class SupportsReadline(Protocol):
    def readline(self) -> str:
        ...

def func(name: str, stream: SupportsReadline) -> None:
    pass

Now any object with a readline method with a compatible signature is an implicit subtype of SupportsReadline and satisfies the annotation of your function argument. Note that LineRepeater does not inherit explicitly from SupportsReadline:

class LineRepeater:
    def readline(self) -> str:
        return "Hello again!"

func("a", LineRepeater())  # OK

The same holds for other objects if the method signature matches exactly:

from io import BytesIO, StringIO

func("a", StringIO())  # OK
func("a", open("foo.txt"))  # OK
func("a", BytesIO())  # ERROR (return type is bytes instead of str)
func("a", [])  # ERROR
func("a", 1)  # ERROR
func("a", object())  # ERROR


问:
Reference array keys without knowing their names?
If I have an array like this:

```<code>$array = array('something'=>array('more'=>array('id'=> 34)));
</code>```

Then print_r($array['something']['more']['id'] works fine.

But say the key names might change but the structure wont. How could I reference the same values without knowing the names?

I thought maybe print_r($array[0][1][2] might work, but of course those keys don't exist.

答:
you can do a straight loop with foreach, although it's fairly ugly:

```<code>foreach ( $grandparent as $gpkey => $parent ) {
    foreach ( $parent as $pkey => $child ) {
        foreach ( $child as $ckey => $value ) {
            print $gpkey . " - " . $pkey . " - " . $ckey . " = " . $value;
        }
    }
}
</code>```

Or you can get the list of keys with array_keys():

```<code>$keys = array_keys($array);
for ( $i=0, $imax=count($keys); $i<$imax; $i++ ) {
    print $key . " = " . $array[$key];
}
</code>```

Body:
If I have an array like this:

$array = array('something'=>array('more'=>array('id'=> 34)));

Then print_r($array['something']['more']['id'] works fine.

But say the key names might change but the structure wont. How could I reference the same values without knowing the names?

I thought maybe print_r($array[0][1][2] might work, but of course those keys don't exist.

Body:
you can do a straight loop with foreach, although it's fairly ugly:

foreach ( $grandparent as $gpkey => $parent ) {
    foreach ( $parent as $pkey => $child ) {
        foreach ( $child as $ckey => $value ) {
            print $gpkey . " - " . $pkey . " - " . $ckey . " = " . $value;
        }
    }
}

Or you can get the list of keys with array_keys():

$keys = array_keys($array);
for ( $i=0, $imax=count($keys); $i<$imax; $i++ ) {
    print $key . " = " . $array[$key];
}


问:
loop in web scraper
The script should find the addresses of subpages with articles and collect the necessary data from them. The data should go to the database. Data should be collected by processing HTML documents.

exactly what it should: 1. find the 10 most common words with their numbers. 2. find The 10 most common words with their numbers per author. 3. posts authors with their name.

I have not finished words counter yet, but at the moment I have with two loops (2.1, 2.2) this should go into every article and get the content from them and authors names.

I get such an error:

```<code>UserWarning: "link/" looks like a URL. Beautiful Soup is not an HTTP client.
You should probably use an HTTP client like requests to get the
document behind the URL, and feed that document to Beautiful Soup.
' that document to Beautiful Soup.' % decoded_markup
</code>```

This is my script:

```<code>import requests
from bs4 import BeautifulSoup as bs
from selenium import webdriver

url = 'https://teonite.com/blog/page/{}/index.html'
all_links = []

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'User-Agent': 'Mozilla/5.0'
}
with requests.Session() as s:
    r = s.get('https://teonite.com/blog/')
    soup = bs(r.content, 'lxml')
    article_links = ['https://teonite.com' + item['href'][2:] for item in soup.select('.post-content a')]
    all_links.append(article_links)
    num_pages = int(soup.select_one('.page-number').text.split('/')[1])

    for page in range(2, num_pages + 1):
        r = s.get(url.format(page))
        soup = bs(r.content, 'lxml')
        article_links = ['https://teonite.com' + item['href'][2:] for item in soup.select('.post-content a')]
        all_links.append(article_links)

    all_links = [item for i in all_links for item in i]

    d = webdriver.Chrome()

    for article in all_links:
        d.get(article)
        soup = bs(d.page_source, 'lxml')
        [t.extract() for t in soup(['style', 'script', '[document]', 'head', 'title'])]
        visible_text = soup.getText()

        try:
            print(soup.select_one('.post-title').text)
        except:
            print(article)
            print(soup.select_one('h1').text)
            break
    d = webdriver.Chrome()

    for article_links in all_links:
        d.get(article)
        soup = bs(d.page_source, 'lxml')

    # nie moj !!!!!!

    # 2.2. Post contents
    contents = []
    for all_links in article_links:
        soup = bs((article), 'html.parser')
        content = soup.find('section', attrs={'class': 'post-content'})
        contents.append(content)

    # 2.1. Authors

    authors = []
    for all_links in article:
        soup = bs(article, 'html.parser')
        author = soup.find('span', attrs={'class': 'author-content'})
        authors.append(author)

    # POSTGRESQL CONNECTION
    # 1. Connect to local database using psycopg2

    import psycopg2

    hostname = 'balarama.db.elephantsql.com'
    username = 'yagoiucf'
    password = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    database = 'yagoiucf'

    conn = psycopg2.connect(host='balarama.db.elephantsql.com', user='yagoiucf',
                            password='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', dbname='yagoiucf')
    conn.close()
</code>```

答:
There are numerous places where you are using a string generated during a prior loop instead of your current loop variable e.g.

```<code>for all_links in article_links:
        soup = bs((article), 'html.parser')
</code>```

article is a url generated in an earlier loop (and quite possibly the source of your error)

Also, all_links is a list of links (flattened from list of lists) which now has been changed to your loop variable for article_links

At this point

```<code>for all_links in article:
    soup = bs(article, 'html.parser')
</code>```

you are passing an url string to bs instead of html I believe.

You also re-create an instance of webdriver when you should continue with the existing instance.

I think you only need the one loop with selenium over the original

```<code>for article in all_links:
</code>```

This visits all the blogs. You can extract whatever content you need whilst on that page within the loop.

Title: loop in web scraper

Body:
The script should find the addresses of subpages with articles and collect the necessary data from them. The data should go to the database. Data should be collected by processing HTML documents.

exactly what it should: 1. find the 10 most common words with their numbers. 2. find The 10 most common words with their numbers per author. 3. posts authors with their name.

I have not finished words counter yet, but at the moment I have with two loops (2.1, 2.2) this should go into every article and get the content from them and authors names.

I get such an error:

UserWarning: "link/" looks like a URL. Beautiful Soup is not an HTTP client.
You should probably use an HTTP client like requests to get the
document behind the URL, and feed that document to Beautiful Soup.
' that document to Beautiful Soup.' % decoded_markup

This is my script:

import requests
from bs4 import BeautifulSoup as bs
from selenium import webdriver

url = 'https://teonite.com/blog/page/{}/index.html'
all_links = []

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'User-Agent': 'Mozilla/5.0'
}
with requests.Session() as s:
    r = s.get('https://teonite.com/blog/')
    soup = bs(r.content, 'lxml')
    article_links = ['https://teonite.com' + item['href'][2:] for item in soup.select('.post-content a')]
    all_links.append(article_links)
    num_pages = int(soup.select_one('.page-number').text.split('/')[1])

    for page in range(2, num_pages + 1):
        r = s.get(url.format(page))
        soup = bs(r.content, 'lxml')
        article_links = ['https://teonite.com' + item['href'][2:] for item in soup.select('.post-content a')]
        all_links.append(article_links)

    all_links = [item for i in all_links for item in i]

    d = webdriver.Chrome()

    for article in all_links:
        d.get(article)
        soup = bs(d.page_source, 'lxml')
        [t.extract() for t in soup(['style', 'script', '[document]', 'head', 'title'])]
        visible_text = soup.getText()

        try:
            print(soup.select_one('.post-title').text)
        except:
            print(article)
            print(soup.select_one('h1').text)
            break
    d = webdriver.Chrome()

    for article_links in all_links:
        d.get(article)
        soup = bs(d.page_source, 'lxml')

    # nie moj !!!!!!

    # 2.2. Post contents
    contents = []
    for all_links in article_links:
        soup = bs((article), 'html.parser')
        content = soup.find('section', attrs={'class': 'post-content'})
        contents.append(content)

    # 2.1. Authors

    authors = []
    for all_links in article:
        soup = bs(article, 'html.parser')
        author = soup.find('span', attrs={'class': 'author-content'})
        authors.append(author)

    # POSTGRESQL CONNECTION
    # 1. Connect to local database using psycopg2

    import psycopg2

    hostname = 'balarama.db.elephantsql.com'
    username = 'yagoiucf'
    password = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    database = 'yagoiucf'

    conn = psycopg2.connect(host='balarama.db.elephantsql.com', user='yagoiucf',
                            password='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', dbname='yagoiucf')
    conn.close()

Body:
There are numerous places where you are using a string generated during a prior loop instead of your current loop variable e.g.

for all_links in article_links:
        soup = bs((article), 'html.parser')

article is a url generated in an earlier loop (and quite possibly the source of your error)

Also, all_links is a list of links (flattened from list of lists) which now has been changed to your loop variable for article_links

At this point

for all_links in article:
    soup = bs(article, 'html.parser')

you are passing an url string to bs instead of html I believe.

You also re-create an instance of webdriver when you should continue with the existing instance.

I think you only need the one loop with selenium over the original

for article in all_links:

This visits all the blogs. You can extract whatever content you need whilst on that page within the loop.


问:
How do I search a list of characters in a text
I have to search the non-English text in a text fields in Power Query. Several language has special characters which might be searchable in the text field and if found then is obvious those text field is Non-English text.

I've tried and works to find specific language characters like "é" or "ä" etc. But I know to use only the Conditional Column function search each character and if found it then gives "0" as value than later I am multiplying all conditional column and if it is 0 then is Non-English text if it is "1" then is English.

```<code>= Table.AddColumn(#"Added Conditional Column13", "Custom É", each if Text.Contains([Text 1], "É") then 0 else if Text.Contains([Text 2], "É") then 0 else if Text.Contains([Text 3], "É") then 0 else 1)
</code>```

Is there a solution to search a list of special characters in the text fields (e.g. "ÁáÉéÖöÜüäß") -> search > results "0" if found anyone in the text otherwise gives as output "1".

答:
You can use List.ContainsAny to check if a list contains any values from a second list.

Combine this with Text.ToList to split characters into a list, then you have something like:

```<code>= Table.AddColumn(#"Prior Step", "Contains Special Characters", each List.ContainsAny(Text.ToList([Values]),Text.ToList("ÁáÉéÖöÜüäß")), type logical)
</code>```

Of course, this approach requires you to provide the list of "special" characters you wish to test for. You could flip this round, and specify the "English" characters:

```<code>#"English Characters" = "abcdefghijklmnopqrstuvwxyz0123456789 ,.:;", // include all characters you consider "English"
#"Added Custom" = Table.AddColumn(#"Prior Step", "Contains Special Characters", each not List.ContainsAny(Text.ToList([Values]),List.Union({Text.ToList(#"English Characters"), Text.ToList(Text.Upper(#"English Characters"))})), type logical)
</code>```

Body:
I have to search the non-English text in a text fields in Power Query. Several language has special characters which might be searchable in the text field and if found then is obvious those text field is Non-English text.

I've tried and works to find specific language characters like "é" or "ä" etc. But I know to use only the Conditional Column function search each character and if found it then gives "0" as value than later I am multiplying all conditional column and if it is 0 then is Non-English text if it is "1" then is English.

= Table.AddColumn(#"Added Conditional Column13", "Custom É", each if Text.Contains([Text 1], "É") then 0 else if Text.Contains([Text 2], "É") then 0 else if Text.Contains([Text 3], "É") then 0 else 1)

Is there a solution to search a list of special characters in the text fields (e.g. "ÁáÉéÖöÜüäß") -> search > results "0" if found anyone in the text otherwise gives as output "1".

Body:
You can use List.ContainsAny to check if a list contains any values from a second list.

Combine this with Text.ToList to split characters into a list, then you have something like:

= Table.AddColumn(#"Prior Step", "Contains Special Characters", each List.ContainsAny(Text.ToList([Values]),Text.ToList("ÁáÉéÖöÜüäß")), type logical)

Of course, this approach requires you to provide the list of "special" characters you wish to test for. You could flip this round, and specify the "English" characters:

#"English Characters" = "abcdefghijklmnopqrstuvwxyz0123456789 ,.:;", // include all characters you consider "English"
#"Added Custom" = Table.AddColumn(#"Prior Step", "Contains Special Characters", each not List.ContainsAny(Text.ToList([Values]),List.Union({Text.ToList(#"English Characters"), Text.ToList(Text.Upper(#"English Characters"))})), type logical)


问:
How to pass variable from java to javascript?
I have a edit funtion in javascript jsp in which i need to pass the id of a book to the java method. In the java method, i use that id to search through a table from database and to find the type of book. (category)
Then, I need to go back to the jsp (javascript function) and to load that type of book into a field.

JAVASCRIPT inside JSP:

```<code><script>
function edit(id) {
        jQuery.ajax({
            type: "GET",
            url: "getId",
            data: "id= " + id,
            datatype: "text"
        });
        var type =<%= ((String)request.getAttribute("myType"))%> ;
        console.log("type is " + type);
    }
</script>
</code>```

JAVA:

```<code>    @RequestMapping("/getId")
    public void getId(
            @RequestParam int id,HttpServletRequest request) {
        idBook = id;
        System.out.println("get id book "+id);
        String type= BookDao.getTypeCategory(id);
        request.setAttribute("myType",type);
        System.out.println("request attribute"+request.getAttribute("myType"));
    }
</code>```

By doing so, the type from javascript is null... How to change that? (the type from java is holding the wanted value). BookDao.getTypeCategory is using the id to search through the database table and to retrieve the needed type.

答:
You need to use @ResponseBody and inside the ajax use the success callback to get the value of success of ajax.

DataTypeOfReturn is the type of the data which you want to return & it can be int/String

```<code>function edit(id) {
  jQuery.ajax({
    type: "GET",
    url: "getId",
    data: "id= " + id,
    datatype: "text",
    success: function(data) {
      console.log(data)
    }
  });
  var type = <%= ((String)request.getAttribute("myType"))%>;
  console.log("type is " + type);
}

@RequestMapping("/getId")
public @ResponseBody DataTypeOfReturn getId(
  @RequestParam int id, HttpServletRequest request) {
  int idBook = id; // add data type here to avoid java error
  System.out.println("get id book " + id);
  String type = BookDao.getTypeCategory(id);
  request.setAttribute("myType", type);
  System.out.println("request attribute" + request.getAttribute("myType"));
  return theValue; // value which you want to return
}
</code>```

Body:
I have a edit funtion in javascript jsp in which i need to pass the id of a book to the java method. In the java method, i use that id to search through a table from database and to find the type of book. (category)
Then, I need to go back to the jsp (javascript function) and to load that type of book into a field.

JAVASCRIPT inside JSP:

JAVA:

    @RequestMapping("/getId")
    public void getId(
            @RequestParam int id,HttpServletRequest request) {
        idBook = id;
        System.out.println("get id book "+id);
        String type= BookDao.getTypeCategory(id);
        request.setAttribute("myType",type);
        System.out.println("request attribute"+request.getAttribute("myType"));
    }

By doing so, the type from javascript is null... How to change that? (the type from java is holding the wanted value). BookDao.getTypeCategory is using the id to search through the database table and to retrieve the needed type.

Body:
You need to use @ResponseBody and inside the ajax use the success callback to get the value of success of ajax.

DataTypeOfReturn is the type of the data which you want to return & it can be int/String

function edit(id) {
  jQuery.ajax({
    type: "GET",
    url: "getId",
    data: "id= " + id,
    datatype: "text",
    success: function(data) {
      console.log(data)
    }
  });
  var type = <%= ((String)request.getAttribute("myType"))%>;
  console.log("type is " + type);
}

@RequestMapping("/getId")
public @ResponseBody DataTypeOfReturn getId(
  @RequestParam int id, HttpServletRequest request) {
  int idBook = id; // add data type here to avoid java error
  System.out.println("get id book " + id);
  String type = BookDao.getTypeCategory(id);
  request.setAttribute("myType", type);
  System.out.println("request attribute" + request.getAttribute("myType"));
  return theValue; // value which you want to return
}


问:
Asp.net MVC Force update of javascript file in a bundle
When I'm developing a web page, I usually include a parameter after the .js reference every time I changed it, to force the browser to get the new version and prevent it to keep with the old one.

For instance, if my reference is like this:

```<code><script src="myfile.js" />
</code>```

The browser will cache the javascript file, and when I publish a new one, it won't get, keeping the old one. So I change the reference to:

```<code><script src="myfile.js?ver=2" />
</code>```

Since the browser index the cache by URL, it will think it is a different file and get it again.

But how can I do that if my javascript is in an asp.net bundle?

```<code>bundles.Add(new ScriptBundle("~/bundles/global").Include(
               "~/Scripts/my/global.js"));
</code>```

I tried the below code but didn't work. How can I force the browser to download the script again in this case?

```<code>bundles.Add(new ScriptBundle("~/bundles/global").Include(
               "~/Scripts/my/global.js?ver=2",
</code>```

答:
Personally, I can't stand the bundler, and have always managed my scripts manually.
So this answer might not be exactly what you're looking for, but will help address having to manually update the ?ver=# every time.

Create a class, perhaps FileVersionRepo that on application start (inside Global.asax) will contain a private static Dictionary<string,string> where the key is a file name, and the value is the FileInfo.LastWriteTime.ToFileTime().ToString() value. This will read the file's lastWriteTime only once when the App Pool restarts, or app is published.

This is also handy for CSS files.

```<code>public static class FileVersionRepo {
    private static Object _lock = new Object();
    private static Dictionary<string,string> fileVals = new Dictionary<string,string>();

    public string GetVal(string key) { return fileVals[key]; }
    public string SetVal(string key, string path) {
        lock (_lock)
        {
            var fileInfo = new FileInfo(path);
            if (fileInfo.Exists)
                FileWriteTimes[key] = fileInfo.LastWriteTime.ToFileTime().ToString();
            else
                return;// maybe throw exception here?
        }
    }
}
</code>```

Then in your Global.asax.cs:

```<code>protected virtual void Application_Start(object sender, EventArgs e)
{
    ...
    FileVersionRepo.SetVal("global.js", "path/global.js");
}
</code>```

Then in your View:

```<code>using FileVersionRepo;

<script src="global.js?ver=@FileVersionRepo.GetVal("global.js")"><script>
</code>```

Body:
Personally, I can't stand the bundler, and have always managed my scripts manually.
So this answer might not be exactly what you're looking for, but will help address having to manually update the ?ver=# every time.

Create a class, perhaps FileVersionRepo that on application start (inside Global.asax) will contain a private static Dictionary<string,string> where the key is a file name, and the value is the FileInfo.LastWriteTime.ToFileTime().ToString() value. This will read the file's lastWriteTime only once when the App Pool restarts, or app is published.

This is also handy for CSS files.

public static class FileVersionRepo {
    private static Object _lock = new Object();
    private static Dictionary<string,string> fileVals = new Dictionary<string,string>();

    public string GetVal(string key) { return fileVals[key]; }
    public string SetVal(string key, string path) {
        lock (_lock)
        {
            var fileInfo = new FileInfo(path);
            if (fileInfo.Exists)
                FileWriteTimes[key] = fileInfo.LastWriteTime.ToFileTime().ToString();
            else
                return;// maybe throw exception here?
        }
    }
}

Then in your Global.asax.cs:

protected virtual void Application_Start(object sender, EventArgs e)
{
    ...
    FileVersionRepo.SetVal("global.js", "path/global.js");
}

Then in your View:

using FileVersionRepo;

<script src="global.js?ver=@FileVersionRepo.GetVal("global.js")"><script>


问:
how to iterate a function over multiple lists?
I have the following function:

```<code>def calc(X: float, Y: float, z: float) -> dict:
    n1 = X*Y
    n2 = Y*z
    n3 = z**2
    return {"c": n1, "d" : n2, "e" : n3}
</code>```

I can take a list such as [10,10,20] and input it into the function using the *args approach:

```<code>list_1 = [10,10,20]
calc(*list_1)

#results
{'c' : 100, 'd': 200, 'e': 400}
</code>```

However, let's assume I want to pass an array or multiple lists into the function, such as:

```<code>list_2 = [[10,10,20],
          [20,25,30],
          [35,40,45]]
</code>```

where I want the function to iterate over each individual list.

I have thought about using a loop, but cannot get how to iterate over each aspect of the multi-dimensional list.

I have tried potentially using the index by doing the following:

```<code>for x in len(list_2):
      call(*list_2[x])
</code>```

but I get the following error:

```<code>'int' object is not ierable
</code>```

Ideally, I would like this to be vectorized and avoid using loops, but any input or direction is much appreciated!

答:
You can use itertools.starmap:</p>

```<code>import itertools

result = itertools.starmap(call, list_2)
</code>```

Body:
I have the following function:

def calc(X: float, Y: float, z: float) -> dict:
    n1 = X*Y
    n2 = Y*z
    n3 = z**2
    return {"c": n1, "d" : n2, "e" : n3}

I can take a list such as [10,10,20] and input it into the function using the *args approach:

list_1 = [10,10,20]
calc(*list_1)

#results
{'c' : 100, 'd': 200, 'e': 400}

However, let's assume I want to pass an array or multiple lists into the function, such as:

list_2 = [[10,10,20],
          [20,25,30],
          [35,40,45]]

where I want the function to iterate over each individual list.

I have thought about using a loop, but cannot get how to iterate over each aspect of the multi-dimensional list.

I have tried potentially using the index by doing the following:

for x in len(list_2):
      call(*list_2[x])

but I get the following error:

'int' object is not ierable

Ideally, I would like this to be vectorized and avoid using loops, but any input or direction is much appreciated!

Body:
You can use itertools.starmap:

import itertools

result = itertools.starmap(call, list_2)


问:
Creating NumPy matrix of a given size in a vectorized manner
If there a built-in NumPy function or any vectorized approach to generate the following 2D NumPy matrices for value of n > 1? Let me give some examples for the desired matrices for n equal to 2, 3, and 4.

One approach would be to construct a matrix of 1’s in the lower triangular part and then simply multiply by a vector np.arange(1, n+1). Any other alternatives?

```<code>import numpy as np
</code>```

For n = 2

```<code>n = 2
arr = np.array([[1, 0], [2, 2]])

# array([[1, 0],
#        [2, 2]])
</code>```

For n = 3

```<code>n = 3
arr = np.array([[1, 0, 0], [2, 2, 0], [3, 3, 3]])

# array([[1, 0, 0],
#        [2, 2, 0],
#        [3, 3, 3]])
</code>```

For n = 4

```<code>n = 4
arr = np.array([[1, 0, 0, 0], [2, 2, 0, 0], [3, 3, 3, 0], [4, 4, 4, 4]])

# array([[1, 0, 0, 0],
#        [2, 2, 0, 0],
#        [3, 3, 3, 0],
#        [4, 4, 4, 4]])
</code>```

My attempt (example for n = 4) using nested for loops. It works for any value of n > 1:

```<code>import numpy as np

n = 4
arr = np.zeros((n, n))

for i in range(n):
    for j in range(0, i+1):
        arr[i][j] = i+1

# array([[1., 0., 0., 0.],
#        [2., 2., 0., 0.],
#        [3., 3., 3., 0.],
#        [4., 4., 4., 4.]])
</code>```

答:
We could use ranged array multiplied by the lower-triangular mask -

```<code>In [44]: n = 5

In [45]: np.arange(1,n+1)[:,None]*np.tri(n,dtype=bool)
Out[45]:
array([[1, 0, 0, 0, 0],
       [2, 2, 0, 0, 0],
       [3, 3, 3, 0, 0],
       [4, 4, 4, 4, 0],
       [5, 5, 5, 5, 5]])
</code>```

It's easily portable to numexpr to leverage multi-cores for large data given the arithmetic-oriented nature -

```<code>import numexpr as ne

ne.evaluate('A*B',{'A':np.arange(1,n+1)[:,None],'B':np.tri(n,dtype=bool)})
</code>```

Benchmarking

Including all posted solutions here.

Benchmarking script -

```<code>import numpy as np
import perfplot

import numexpr as ne

def numexpr_range_broadcast(n):
    return ne.evaluate('A*B',{'A':np.arange(1,n+1)[:,None],'B':np.tri(n,dtype=bool)})

def where_method(n):
    x = np.arange(1,n+1)
    return np.where(x[:,None]>=x,x[:,None],0)

perfplot.show(
    setup=lambda n: n,
    kernels=[
        lambda n: where_method(n),
        lambda n: np.tril(np.broadcast_to(np.arange(1, n+1)[:, None], (n, n))),
        lambda n: np.arange(1,n+1)[:,None]*np.tri(n,dtype=bool),
        lambda n: numexpr_range_broadcast(n),
        ],
    labels=['where','tril_broadacast','range_broadcast','numexpr_range_broadcast'],
    n_range=[10, 20, 50, 80, 100, 200, 500, 800, 1000, 2000, 5000],
    xlabel='n',
    logx=True,
    logy=True,
    )
</code>```

Output -

Hence, on lower n's little before 100, np.where based one wins it and on larger n's, numexpr shines.

Body:
If there a built-in NumPy function or any vectorized approach to generate the following 2D NumPy matrices for value of n > 1? Let me give some examples for the desired matrices for n equal to 2, 3, and 4.

One approach would be to construct a matrix of 1’s in the lower triangular part and then simply multiply by a vector np.arange(1, n+1). Any other alternatives?

import numpy as np

For n = 2

n = 2
arr = np.array([[1, 0], [2, 2]])

# array([[1, 0],
#        [2, 2]])

For n = 3

n = 3
arr = np.array([[1, 0, 0], [2, 2, 0], [3, 3, 3]])

# array([[1, 0, 0],
#        [2, 2, 0],
#        [3, 3, 3]])

For n = 4

n = 4
arr = np.array([[1, 0, 0, 0], [2, 2, 0, 0], [3, 3, 3, 0], [4, 4, 4, 4]])

# array([[1, 0, 0, 0],
#        [2, 2, 0, 0],
#        [3, 3, 3, 0],
#        [4, 4, 4, 4]])

My attempt (example for n = 4) using nested for loops. It works for any value of n > 1:

import numpy as np

n = 4
arr = np.zeros((n, n))

for i in range(n):
    for j in range(0, i+1):
        arr[i][j] = i+1

# array([[1., 0., 0., 0.],
#        [2., 2., 0., 0.],
#        [3., 3., 3., 0.],
#        [4., 4., 4., 4.]])

Body:
We could use ranged array multiplied by the lower-triangular mask -

In [44]: n = 5

In [45]: np.arange(1,n+1)[:,None]*np.tri(n,dtype=bool)
Out[45]:
array([[1, 0, 0, 0, 0],
       [2, 2, 0, 0, 0],
       [3, 3, 3, 0, 0],
       [4, 4, 4, 4, 0],
       [5, 5, 5, 5, 5]])

It's easily portable to numexpr to leverage multi-cores for large data given the arithmetic-oriented nature -

import numexpr as ne

ne.evaluate('A*B',{'A':np.arange(1,n+1)[:,None],'B':np.tri(n,dtype=bool)})

Benchmarking

Including all posted solutions here.

Benchmarking script -

import numpy as np
import perfplot

import numexpr as ne

def numexpr_range_broadcast(n):
    return ne.evaluate('A*B',{'A':np.arange(1,n+1)[:,None],'B':np.tri(n,dtype=bool)})

def where_method(n):
    x = np.arange(1,n+1)
    return np.where(x[:,None]>=x,x[:,None],0)

perfplot.show(
    setup=lambda n: n,
    kernels=[
        lambda n: where_method(n),
        lambda n: np.tril(np.broadcast_to(np.arange(1, n+1)[:, None], (n, n))),
        lambda n: np.arange(1,n+1)[:,None]*np.tri(n,dtype=bool),
        lambda n: numexpr_range_broadcast(n),
        ],
    labels=['where','tril_broadacast','range_broadcast','numexpr_range_broadcast'],
    n_range=[10, 20, 50, 80, 100, 200, 500, 800, 1000, 2000, 5000],
    xlabel='n',
    logx=True,
    logy=True,
    )

Output -

Hence, on lower n's little before 100, np.where based one wins it and on larger n's, numexpr shines.


问:
UITextview does not scroll to top
I am using a UITextView to display a long text in ViewDidLoad():

```<code>var story1 = "Researchers on Friday introduced the stunningly realistic-looking interactive humanoid robot to the world, and she reacted by nodding her head, squinting, moving her hand and eyeballs and telling assembled local photographers, with her speech synched to her lip movements, to snap her face from a more flattering angle. Girl's got spirit. \n\nJia Jia made her public debut at the University of Science and Technology of China, where she came to life over the course of three years. While her rosy cheeks and pink lips make her look like she has blood running through her actuators, she still has a way to go before she'll be confused with a human. Her expressions are currently limited to 'microexpressions' and her speech has that distinct stiffness that screams 'I am a robot and I will one day chase you from your home'\n\......." // a very long text with many rows
self.storyTextView.text = story1
</code>```

If I set Scrolling Enabled for storyTextView, the textview displays the text not from the beginning. I can see the scroll indicator in the middle (or bottom) of the text.

see screenshot1.

I tried

```<code>self.storyTextView.scrollsToTop = true
</code>```

but it does not help.

If I uncheck Scrolling Enabled, or if the text is not too long to display whole text in a UITextView, it displays from the beginning as expected. See screenshot2.

Is there any way to display the long text from the beginning of the text (move scroll indicator to the top)? I checked constraints and layout but it seems not related. I did search a lot but no luck until now. I might missed some simple setting but anyone can help me out please.

答:
In my case this solution is work, Swift 4: </p>

```<code>override func viewDidLayoutSubviews() {
    self.txt_about.setContentOffset(CGPoint.zero, animated: false)
}
</code>```

Title: UITextview does not scroll to top

Body:
I am using a UITextView to display a long text in ViewDidLoad():

var story1 = "Researchers on Friday introduced the stunningly realistic-looking interactive humanoid robot to the world, and she reacted by nodding her head, squinting, moving her hand and eyeballs and telling assembled local photographers, with her speech synched to her lip movements, to snap her face from a more flattering angle. Girl's got spirit. \n\nJia Jia made her public debut at the University of Science and Technology of China, where she came to life over the course of three years. While her rosy cheeks and pink lips make her look like she has blood running through her actuators, she still has a way to go before she'll be confused with a human. Her expressions are currently limited to 'microexpressions' and her speech has that distinct stiffness that screams 'I am a robot and I will one day chase you from your home'\n\......." // a very long text with many rows
self.storyTextView.text = story1

If I set Scrolling Enabled for storyTextView, the textview displays the text not from the beginning. I can see the scroll indicator in the middle (or bottom) of the text.

see screenshot1.

I tried

self.storyTextView.scrollsToTop = true

but it does not help.

If I uncheck Scrolling Enabled, or if the text is not too long to display whole text in a UITextView, it displays from the beginning as expected. See screenshot2.

Is there any way to display the long text from the beginning of the text (move scroll indicator to the top)? I checked constraints and layout but it seems not related. I did search a lot but no luck until now. I might missed some simple setting but anyone can help me out please.

Body:
In my case this solution is work, Swift 4:

override func viewDidLayoutSubviews() {
    self.txt_about.setContentOffset(CGPoint.zero, animated: false)
}


问:
Is there a way to beautify curl command in Vscode?
I've a complex curl command that I want to analyze but it's all minfied into one line. Is there a formatter that would beautify it?</p>

答:
```<code>curl http://example.com/api/getPosts | node <<< "var o = $(cat); console.log(JSON.stringify(o, null, 4));"
</code>```

Got it from here: https://stackoverflow.com/a/27238477/2159159

Body:
curl http://example.com/api/getPosts | node <<< "var o = $(cat); console.log(JSON.stringify(o, null, 4));"

Got it from here: https://stackoverflow.com/a/27238477/2159159


Title: how to iterate a function over multiple lists?

Body:
You could also pass unpacked list_2 into map() function.

Code:

def calc(x: float, y: float, z: float) -> dict:
    n1 = x*y
    n2 = y*z
    n3 = z**2
    return {"c": n1, "d": n2, "e": n3}

list_2 = [[10, 10, 20], [20, 25, 30], [35, 40, 45]]

result = list(map(calc, *list_2))

Output:

[{'c': 200, 'd': 700, 'e': 1225}, {'c': 250, 'd': 1000, 'e': 1600}, {'c': 600, 'd': 1350, 'e': 2025}]


问:
all inputs of a certain class to json
I have this type of html:

```<code><input id='a1' name='a1' value='111' class='hey'/>
<input id='b1' name='b1' value='222' class='hey'/>
<input id='c1' name='b1' value='333' class='hey'/>
</code>```

and I need to obtain this json from it:

```<code>{ a1: '111', b1: '222', c1: '333' }
</code>```

anybody knows how ?

答:
That should be pretty easy:

```<code>var json = {};
$('input.hey').each(function() {
  json[this.name] = this.value;
});
</code>```

There are serialization routines supplied by jQuery to (for example) vaccuum up a whole form, but in a case like this it's pretty easy to just roll your own.

Title: all inputs of a certain class to json

Body:
I have this type of html:

<input id='a1' name='a1' value='111' class='hey'/>
<input id='b1' name='b1' value='222' class='hey'/>
<input id='c1' name='b1' value='333' class='hey'/>

and I need to obtain this json from it:

{ a1: '111', b1: '222', c1: '333' }

anybody knows how ?

Body:
That should be pretty easy:

var json = {};
$('input.hey').each(function() {
  json[this.name] = this.value;
});

There are serialization routines supplied by jQuery to (for example) vaccuum up a whole form, but in a case like this it's pretty easy to just roll your own.


问:
Read file in SSIS Project into a variable
My SSIS projects tend to run queries that require changes as they move between environments, like the table schema might change or a value in the Where clause.  I've always either put my SQL into a Project Parameter, which is hard to edit since formatting is lost, or just put it directly into the Execute SQL Task/Data Flow Source then manually edited it between migrations which is also not ideal.

I was wonder though if I added my SQL scripts to files within the project, can these be read back in?  Example if I put a query like this:
select id, name from %schema%.tablename
I'd like to read this into a variable then it's easy to use an expression as I do with Project Parameters to replace %schema% with the appropriate value.  Then the .sql files within the project can be edited with little effort or even tested through an Execute SQL Task that's disabled/removed before the project goes into the deployment flow. But I've been unable to find how to read in a file using a relative path within the project.  Also I'm not even sure these get deployed to the SSIS Server.

Thanks for any insight.

I've added a text file query.sql to an SSIS (SQL 2017) Project in Visual Studio, bit I've found no way to pull the contents of query.sql into a variable.

答:
Native tooling approach

For an Execute SQL Task, there's an option to source your query directly from a file.

Set your SQLSourceType to File Connection and then specify a file connection manager in the FileConnection section.

Do be aware that while this is handy, it's also ripe for someone escalating their permissions. If I had access to the file the SSIS package is looking for, I can add a drop database, create a new user and give them SA rights, etc - anything the account that runs the SSIS package can do, a nefarious person could exploit.

Roll your own approach

If you're adamant about reading the file yourself, add two Variables to your SSIS package and supply values like the following

User::QueryPath -> String -> C:\path\to\file.sql

User::QueryActual -> String -> SELECT 1;

Add a Script Task to the package. Specify as a ReadOnly variable User::QueryPath and specify as a ReadWrite variable User::QueryActual

Within the Main you'd need code like the following

```<code>string filePath = this.Dts.Variables["User::QueryPath"].Value.ToString();
this.Dts.Variables["User::QueryActual"].Value = System.IO.File.ReadAllText(filePath);
</code>```

The meat of the matter is System.IO.File.ReadAllText. Note that this doesn't handle checking whether the file exists, you have permission to access, etc. It's just a barebones read of a file (and also open to the same injection challenges as the above method - just this way you own maintaining it versus the fine engineers at Microsoft)

Body:
Native tooling approach

For an Execute SQL Task, there's an option to source your query directly from a file.

Set your SQLSourceType to File Connection and then specify a file connection manager in the FileConnection section.

Do be aware that while this is handy, it's also ripe for someone escalating their permissions. If I had access to the file the SSIS package is looking for, I can add a drop database, create a new user and give them SA rights, etc - anything the account that runs the SSIS package can do, a nefarious person could exploit.

Roll your own approach

If you're adamant about reading the file yourself, add two Variables to your SSIS package and supply values like the following

User::QueryPath -> String -> C:\path\to\file.sql

User::QueryActual -> String -> SELECT 1;

Add a Script Task to the package. Specify as a ReadOnly variable User::QueryPath and specify as a ReadWrite variable User::QueryActual

Within the Main you'd need code like the following

string filePath = this.Dts.Variables["User::QueryPath"].Value.ToString();
this.Dts.Variables["User::QueryActual"].Value = System.IO.File.ReadAllText(filePath);

The meat of the matter is System.IO.File.ReadAllText. Note that this doesn't handle checking whether the file exists, you have permission to access, etc. It's just a barebones read of a file (and also open to the same injection challenges as the above method - just this way you own maintaining it versus the fine engineers at Microsoft)


问:
Using "Either" in data validation
I have a program which is dedicated to create "Student" (First Name, Last Name, Age) and validate input data. My problem is that: when I insert student which e.g either name hasn't 2 letters or age is lower than 18 - the program only show one error. How to use "Either function" to create for example a string which contains all errors ? </p>

```<code>module Student where

data Student = Student {firstName::FirstName, lastName::LastName, age::Age}
  deriving Show

newtype FirstName = FirstName String
  deriving Show

newtype LastName = LastName String
  deriving Show

newtype Age = Age Int
  deriving Show

mkStudent :: String -> String -> String -> Either String Student
mkStudent fn ln a =
   Student <$> validate fn
           <*> validate ln
           <*> validate a

aceptableLetters = ['a'..'z']++['A'..'Z']

validateFn :: String -> Either String FirstName
validateFn fn
   | length fn < 2 = Left "First name has to at least 2 letters"
   | length fn > 100 = Left "First name is limited to 100 characters"
   | not . null $ filter (\c -> not . elem c $ aceptableLetters) fn = Left "First name contains unacceptable chars"
   | otherwise = Right . FirstName $ fn

validateLn :: String -> Either String LastName
validateLn lastName
   | length lastName < 2 = Left "Last name has to at least 2 letters"
   | length lastName > 100 = Left "Last name is limited to 100 characters"
   | not . null $ filter (\c -> not . elem c $ aceptableLetters) lastName = Left "Last name contains unacceptable chars"
   | otherwise = Right . LastName $ lastName

validateA :: String -> Either String Age
validateA a
   | age <= 18 = Left "Student has to be at least 18"
   | age > 100 = Left "Student has more than 100 years. Probably it is an error."
   | otherwise = Right . Age $ age
   where
    age = read a

class Validate v where
    validate :: String -> Either String v

instance Validate FirstName where
    validate=validateFn

instance Validate LastName where
    validate=validateLn

instance Validate Age where
    validate=validateA
</code>```

答:
An easy fix would be to change the left Either to a custom data type. What I mean is that, instead of having Either String Age, you could have Either ErrorType Age, and then define

```<code>data ErrorType = LowAge | WrongName
</code>```

In my opinion this would also be better programming practice because you'd be separating the error throwing and its handler. Especially in Haskell and functional programming, I wouldn't carry around string representations to handle errors. Then you can for example combine the errors into a list, i.e:

```<code>mkStudent :: String -> String -> String -> Either [ErrorType] Student
</code>```

Then just have another function to handle the list and print it to the user, or log it, or anything you'd want to do with it.

Title: Using "Either" in data validation

Body:
I have a program which is dedicated to create "Student" (First Name, Last Name, Age) and validate input data. My problem is that: when I insert student which e.g either name hasn't 2 letters or age is lower than 18 - the program only show one error. How to use "Either function" to create for example a string which contains all errors ?

module Student where

data Student = Student {firstName::FirstName, lastName::LastName, age::Age}
  deriving Show

newtype FirstName = FirstName String
  deriving Show

newtype LastName = LastName String
  deriving Show

newtype Age = Age Int
  deriving Show

mkStudent :: String -> String -> String -> Either String Student
mkStudent fn ln a =
   Student <$> validate fn
           <*> validate ln
           <*> validate a

aceptableLetters = ['a'..'z']++['A'..'Z']

validateFn :: String -> Either String FirstName
validateFn fn
   | length fn < 2 = Left "First name has to at least 2 letters"
   | length fn > 100 = Left "First name is limited to 100 characters"
   | not . null $ filter (\c -> not . elem c $ aceptableLetters) fn = Left "First name contains unacceptable chars"
   | otherwise = Right . FirstName $ fn

validateLn :: String -> Either String LastName
validateLn lastName
   | length lastName < 2 = Left "Last name has to at least 2 letters"
   | length lastName > 100 = Left "Last name is limited to 100 characters"
   | not . null $ filter (\c -> not . elem c $ aceptableLetters) lastName = Left "Last name contains unacceptable chars"
   | otherwise = Right . LastName $ lastName

validateA :: String -> Either String Age
validateA a
   | age <= 18 = Left "Student has to be at least 18"
   | age > 100 = Left "Student has more than 100 years. Probably it is an error."
   | otherwise = Right . Age $ age
   where
    age = read a

class Validate v where
    validate :: String -> Either String v

instance Validate FirstName where
    validate=validateFn

instance Validate LastName where
    validate=validateLn

instance Validate Age where
    validate=validateA

Body:
An easy fix would be to change the left Either to a custom data type. What I mean is that, instead of having Either String Age, you could have Either ErrorType Age, and then define

data ErrorType = LowAge | WrongName

In my opinion this would also be better programming practice because you'd be separating the error throwing and its handler. Especially in Haskell and functional programming, I wouldn't carry around string representations to handle errors. Then you can for example combine the errors into a list, i.e:

mkStudent :: String -> String -> String -> Either [ErrorType] Student

Then just have another function to handle the list and print it to the user, or log it, or anything you'd want to do with it.


问:
Generating CSV/Excel file for 100 Million of records in Ruby on rails?
Requirement is like

We get the huge dataset from the database( > 1 billion records) and need to export it to the csv file or excel.

Currently implementation use CSV class CSV.generate

```<code> CSV.generate(headers: true) do |csv|
    csv << header
    @obj.find_each do |c|
     arr = [c.id,c.name,soon]
     csv << array
    end
 end
</code>```

and sending the output to

```<code>Zip::File.open(file, Zip::File::CREATE) do |zip|
        zip.get_output_stream("test.#{@format}") { |f| f.puts(convert_to_csv) }
      end
</code>```

All this operation is done other delayed jobs
This works good when record is < 20,000
But when rows starts growing it gets some memory issues.

What i was thinking is to chunk the record to pieces say 1 million rows into 50 files (1million/20000)(csv1.csv,csv2.csv,csv3.csv,csv4.csv,csv5.csv) and then concat them into single file or zip all files together(faster way)

Can any one give me idea how can I start on it.

答:
Taking a look at the source for CSV.generate gives me the impression that the csv data is kept in memory while the contents are being accumulated.  That seems like a good target for optimization, especially if you see that memory is scaling linearly with the data set.  Since your data is pretty simple, could you skip CSV and go directly to File instead?  You'd have a bit more control about when data was flushed out to disk.

```<code>File.open("my.csv") do |file|
  file.puts '"ID","Name","Soon"'
  @obj.find_each do |c|
    file.puts "\"#{c.id}\",\"#{c.name}\",\"#{c.soon}\""
    # flush if necessary
  end
end
</code>```

You'd need to write to disk and then zip the results later with this approach.

Body:
Requirement is like

We get the huge dataset from the database( > 1 billion records) and need to export it to the csv file or excel.

Currently implementation use CSV class CSV.generate

 CSV.generate(headers: true) do |csv|
    csv << header
    @obj.find_each do |c|
     arr = [c.id,c.name,soon]
     csv << array
    end
 end

and sending the output to

Zip::File.open(file, Zip::File::CREATE) do |zip|
        zip.get_output_stream("test.#{@format}") { |f| f.puts(convert_to_csv) }
      end

All this operation is done other delayed jobs
This works good when record is < 20,000
But when rows starts growing it gets some memory issues.

What i was thinking is to chunk the record to pieces say 1 million rows into 50 files (1million/20000)(csv1.csv,csv2.csv,csv3.csv,csv4.csv,csv5.csv) and then concat them into single file or zip all files together(faster way)

Can any one give me idea how can I start on it.

Body:
Taking a look at the source for CSV.generate gives me the impression that the csv data is kept in memory while the contents are being accumulated.  That seems like a good target for optimization, especially if you see that memory is scaling linearly with the data set.  Since your data is pretty simple, could you skip CSV and go directly to File instead?  You'd have a bit more control about when data was flushed out to disk.

File.open("my.csv") do |file|
  file.puts '"ID","Name","Soon"'
  @obj.find_each do |c|
    file.puts "\"#{c.id}\",\"#{c.name}\",\"#{c.soon}\""
    # flush if necessary
  end
end

You'd need to write to disk and then zip the results later with this approach.


问:
Using conda install within a python script
According to this answer you can import pip from within a Python script and use it to install a module. Is it possible to do this with conda install?

The conda documentation only shows examples from the command line but I'm looking for code that can be executed from within a Python script.

Yes, I could execute shell commands from within the script but I am trying to avoid this as it is basically assuming that conda cannot be imported and its functions called.

答:
Having worked with conda from Python scripts for a while now, I think calling conda with the subprocess module works the best overall. In Python 3.7+, you could do something like this:

import json
from subprocess import run

def conda_list(environment):
    proc = run(["conda", "list", "--json", "--name", environment],
               text=True, capture_output=True)
    return json.loads(proc.stdout)

def conda_install(environment, *package):
    proc = run(["conda", "install", "--quiet", "--name", environment] + packages,
               text=True, capture_output=True)
    return json.loads(proc.stdout)
```

As I pointed out in a comment, conda.cli.main() was not intended for external use. It parses sys.argv directly, so if you try to use it in your own script with your own command line arguments, they will get fed to conda.cli.main() as well.

@YenForYang's answer suggesting conda.cli.python_api is better because this is a publicly documented API for calling conda commands. However, I have found that it still has rough edges. conda builds up internal state as it executes a command (e.g. caches). The way conda is usually used and usually tested is as a command line program. In that case, this internal state is discarded at the end of the conda command. With conda.cli.python_api, you can execute several conda commands within a single process. In this case, the internal state carries over and can sometimes lead to unexpected results (e.g. the cache becomes outdated as commands are performed). Of course, it should be possible for conda to handle this internal state directly. My point is just that using conda this way is not the main focus of the developers. If you want the most reliable method, use conda the way the developers intend it to be used -- as its own process.

conda is a fairly slow command, so I don't think one should worry about the performance impact of calling a subprocess. As I noted in another comment, pip is a similar tool to conda and explicitly states in its documentation that it should be called as a subprocess, not imported into Python.

Body:
Having worked with conda from Python scripts for a while now, I think calling conda with the subprocess module works the best overall. In Python 3.7+, you could do something like this:

import json
from subprocess import run

def conda_list(environment):
    proc = run(["conda", "list", "--json", "--name", environment],
               text=True, capture_output=True)
    return json.loads(proc.stdout)

def conda_install(environment, *package):
    proc = run(["conda", "install", "--quiet", "--name", environment] + packages,
               text=True, capture_output=True)
    return json.loads(proc.stdout)

As I pointed out in a comment, conda.cli.main() was not intended for external use. It parses sys.argv directly, so if you try to use it in your own script with your own command line arguments, they will get fed to conda.cli.main() as well.

@YenForYang's answer suggesting conda.cli.python_api is better because this is a publicly documented API for calling conda commands. However, I have found that it still has rough edges. conda builds up internal state as it executes a command (e.g. caches). The way conda is usually used and usually tested is as a command line program. In that case, this internal state is discarded at the end of the conda command. With conda.cli.python_api, you can execute several conda commands within a single process. In this case, the internal state carries over and can sometimes lead to unexpected results (e.g. the cache becomes outdated as commands are performed). Of course, it should be possible for conda to handle this internal state directly. My point is just that using conda this way is not the main focus of the developers. If you want the most reliable method, use conda the way the developers intend it to be used -- as its own process.

conda is a fairly slow command, so I don't think one should worry about the performance impact of calling a subprocess. As I noted in another comment, pip is a similar tool to conda and explicitly states in its documentation that it should be called as a subprocess, not imported into Python.