问:
Best methods for drawing an interactive 2D plot in MacOS
I'm developing a MacOS application in Swift where users record keyframe values against a 2D timeline. My points are stored as a dictionary where the keys are times and the values are the corresponding value at that time.

I have no problem at all plotting my points as a static 2D line graph, and drawing circles on the line to indicate the actual recorded data points. Even with very large numbers of data points it's very snappy to draw lines, even including recalculating and drawing the line in "real-time" during recording.

Where I get stuck is then taking my 2D plot and turning it into an "interactive" plot where the user can select one or more points and then drag them around, altering the stored points in the corresponding data.

What I have been trying so far is drawing my line in my NSView, then instead of drawing the circles for each point as a NSBezierPath, I create an instance of a custom NSView for each point that draws the circle and contains all the mouseDown/mouseDragged/etc. functions and add it as a subview of the main line view. While this "technically" works, it grinds the machine to a halt as it calculates and drops all these views, and obviously more points equals more drag.

Am I missing some way of accomplishing this in a more efficient way? I've been searching and while there are a million and one frameworks and methods for drawing 2D plots of large data sets, I haven't found a single example of one that has "interactive" user-manipulation of the data.

答:
This demo uses a data array of NSPoints to create an interactive graph which allows the data points to be individually moved with the mouse.  It does not address multiple point selection and will be need to be scaled up to suit your needs (I haven’t tested it with more than 20 points; hopefully it won’t bog down).  The demo may be run from the Command Line in Terminal or in Xcode by adding a ‘main.swift’ file and replacing the existing AppDelegate with the corresponding class below.</p>

```<code>// May be run in Terminal using:
// swiftc ptinrect.swift -framework Cocoa -o ptinrect && ./ptinrect
// or in Xcode with instructions above.

import Cocoa

let path = NSBezierPath()
var R = [NSRect]()
var selected = [Bool]()
var data = [NSPoint]()

class CustomView: NSView {

override func draw(_ rect: NSRect ) {
 let bkgrnd = NSBezierPath(rect: rect)
 NSColor.lightGray.set()
 bkgrnd.fill()
// circles
 NSColor.black.set()
 for x in stride(from:0, to:R.count, by:1) {
 let circle = NSBezierPath(ovalIn:R[x])
  circle.fill()
 }
// lines
 NSColor.white.set()
 path.lineWidth = 2.0
 path.move(to:data[0])
 for x in stride(from:0, to:data.count, by:1) {
  path.line(to:data[x])
 }
 path.stroke()
}

override func mouseDown(with event: NSEvent) {
  let wndPt: NSPoint = event.locationInWindow
  let pt:NSPoint = self.convert(wndPt, from: nil)
  print(pt)
  print(R.count)
  for x in stride(from:0, to:R.count, by:1) {
   if NSPointInRect(pt,R[x]){
   selected[x] = true
   print("mouse down in rect: \(x)")
   print([selected])
   } else {selected[x] = false}
  }
}

override func mouseUp(with event: NSEvent) {
 let wndPt: NSPoint = event.locationInWindow
 let pt:NSPoint = self.convert(wndPt, from: nil)
  print(pt)
  for x in stride(from:0, to:R.count, by:1) {
   if NSPointInRect(pt,R[x]){
   print("mouse up in rect: \(x)")
   }
  }
 path.removeAllPoints()
 self.needsDisplay = true
}

override func mouseDragged(with event: NSEvent) {
 let wndPt: NSPoint = event.locationInWindow
 let pt:NSPoint = self.convert(wndPt, from: nil)
  for x in stride(from:0, to:R.count, by:1) {
   if (selected[x] == true){
   print("mouse dragged in rect: \(x)")
   data[x] = pt
   R[x].origin.x = pt.x - 5
   R[x].origin.y = pt.y - 5
   }
  }
 path.removeAllPoints()
 self.needsDisplay = true
}

}

class ApplicationDelegate: NSObject, NSApplicationDelegate {
 var window: NSWindow!

func buildPath() {
var count: CGFloat = 0
 for x in stride(from:0, to:20, by:1) {
  let offset = CGFloat(count * 20.0)
  data.append(NSPoint.init())
  data[x] = NSMakePoint( 20 + offset, 20 + offset)
  R.append(NSRect.init())
  // x,y coordinates -5 to center rect on data pt
  R[x] = NSMakeRect(data[x].x - 5,data[x].y - 5,10,10)
  selected.append(Bool.init())
  selected[x] = false
  count += 1
 }
 print([data])
 path.move(to:data[0])
  for x in stride(from:0, to:data.count, by:1) {
  path.line(to:data[x])
 }
}

func buildMenu() {
let mainMenu = NSMenu()
NSApp.mainMenu = mainMenu
// **** App menu **** //
let appMenuItem = NSMenuItem()
mainMenu.addItem(appMenuItem)
let appMenu = NSMenu()
appMenuItem.submenu = appMenu
appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
}

func buildWnd() {
let _wndW : CGFloat = 800
let _wndH : CGFloat = 600

window = NSWindow(contentRect: NSMakeRect( 0, 0, _wndW, _wndH ), styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)
 window.center()
 window.title = "Swift Test Window"
 window.makeKeyAndOrderFront(window)

// **** Custom view **** //
 let view = CustomView( frame:NSMakeRect(20, 60, _wndW - 40, _wndH - 80))
 view.autoresizingMask = [.maxXMargin,.minYMargin, .height, .width]
 window.contentView!.addSubview (view)

// **** Quit btn **** //
 let quitBtn = NSButton (frame:NSMakeRect( _wndW - 50, 10, 40, 40 ))
 quitBtn.bezelStyle = .circular
 quitBtn.autoresizingMask = [.minXMargin,.maxYMargin]
 quitBtn.title = "Q"
 quitBtn.action = #selector(NSApplication.terminate)
 window.contentView!.addSubview(quitBtn)
}

func applicationDidFinishLaunching(_ notification: Notification) {
 buildMenu()
 buildWnd()
 buildPath()
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
 return true
}

}
let appDelegate = ApplicationDelegate()

// **** main.swift **** //
let application = NSApplication.shared
application.setActivationPolicy(NSApplication.ActivationPolicy.regular)
application.delegate = appDelegate
application.activate(ignoringOtherApps:true)
application.run()

</code>```

Body:
This demo uses a data array of NSPoints to create an interactive graph which allows the data points to be individually moved with the mouse.  It does not address multiple point selection and will be need to be scaled up to suit your needs (I haven’t tested it with more than 20 points; hopefully it won’t bog down).  The demo may be run from the Command Line in Terminal or in Xcode by adding a ‘main.swift’ file and replacing the existing AppDelegate with the corresponding class below.

// May be run in Terminal using:
// swiftc ptinrect.swift -framework Cocoa -o ptinrect && ./ptinrect
// or in Xcode with instructions above.

import Cocoa

let path = NSBezierPath()
var R = [NSRect]()
var selected = [Bool]()
var data = [NSPoint]()

class CustomView: NSView {

override func draw(_ rect: NSRect ) {
 let bkgrnd = NSBezierPath(rect: rect)
 NSColor.lightGray.set()
 bkgrnd.fill()
// circles
 NSColor.black.set()
 for x in stride(from:0, to:R.count, by:1) {
 let circle = NSBezierPath(ovalIn:R[x])
  circle.fill()
 }
// lines
 NSColor.white.set()
 path.lineWidth = 2.0
 path.move(to:data[0])
 for x in stride(from:0, to:data.count, by:1) {
  path.line(to:data[x])
 }
 path.stroke()
}

override func mouseDown(with event: NSEvent) {
  let wndPt: NSPoint = event.locationInWindow
  let pt:NSPoint = self.convert(wndPt, from: nil)
  print(pt)
  print(R.count)
  for x in stride(from:0, to:R.count, by:1) {
   if NSPointInRect(pt,R[x]){
   selected[x] = true
   print("mouse down in rect: \(x)")
   print([selected])
   } else {selected[x] = false}
  }
}

override func mouseUp(with event: NSEvent) {
 let wndPt: NSPoint = event.locationInWindow
 let pt:NSPoint = self.convert(wndPt, from: nil)
  print(pt)
  for x in stride(from:0, to:R.count, by:1) {
   if NSPointInRect(pt,R[x]){
   print("mouse up in rect: \(x)")
   }
  }
 path.removeAllPoints()
 self.needsDisplay = true
}

override func mouseDragged(with event: NSEvent) {
 let wndPt: NSPoint = event.locationInWindow
 let pt:NSPoint = self.convert(wndPt, from: nil)
  for x in stride(from:0, to:R.count, by:1) {
   if (selected[x] == true){
   print("mouse dragged in rect: \(x)")
   data[x] = pt
   R[x].origin.x = pt.x - 5
   R[x].origin.y = pt.y - 5
   }
  }
 path.removeAllPoints()
 self.needsDisplay = true
}

}

class ApplicationDelegate: NSObject, NSApplicationDelegate {
 var window: NSWindow!

func buildPath() {
var count: CGFloat = 0
 for x in stride(from:0, to:20, by:1) {
  let offset = CGFloat(count * 20.0)
  data.append(NSPoint.init())
  data[x] = NSMakePoint( 20 + offset, 20 + offset)
  R.append(NSRect.init())
  // x,y coordinates -5 to center rect on data pt
  R[x] = NSMakeRect(data[x].x - 5,data[x].y - 5,10,10)
  selected.append(Bool.init())
  selected[x] = false
  count += 1
 }
 print([data])
 path.move(to:data[0])
  for x in stride(from:0, to:data.count, by:1) {
  path.line(to:data[x])
 }
}

func buildMenu() {
let mainMenu = NSMenu()
NSApp.mainMenu = mainMenu
// **** App menu **** //
let appMenuItem = NSMenuItem()
mainMenu.addItem(appMenuItem)
let appMenu = NSMenu()
appMenuItem.submenu = appMenu
appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
}

func buildWnd() {
let _wndW : CGFloat = 800
let _wndH : CGFloat = 600

window = NSWindow(contentRect: NSMakeRect( 0, 0, _wndW, _wndH ), styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)
 window.center()
 window.title = "Swift Test Window"
 window.makeKeyAndOrderFront(window)

// **** Custom view **** //
 let view = CustomView( frame:NSMakeRect(20, 60, _wndW - 40, _wndH - 80))
 view.autoresizingMask = [.maxXMargin,.minYMargin, .height, .width]
 window.contentView!.addSubview (view)

// **** Quit btn **** //
 let quitBtn = NSButton (frame:NSMakeRect( _wndW - 50, 10, 40, 40 ))
 quitBtn.bezelStyle = .circular
 quitBtn.autoresizingMask = [.minXMargin,.maxYMargin]
 quitBtn.title = "Q"
 quitBtn.action = #selector(NSApplication.terminate)
 window.contentView!.addSubview(quitBtn)
}

func applicationDidFinishLaunching(_ notification: Notification) {
 buildMenu()
 buildWnd()
 buildPath()
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
 return true
}

}
let appDelegate = ApplicationDelegate()

// **** main.swift **** //
let application = NSApplication.shared
application.setActivationPolicy(NSApplication.ActivationPolicy.regular)
application.delegate = appDelegate
application.activate(ignoringOtherApps:true)
application.run()


来源: stackexchange

问:
I want all rows of the excel file to be saved as a list. What to do
```<code>data=[]
workbook=openpyxl.load_workbook(filename='crawling.xlsx')
sheet=workbook.active
for row in sheet.rows:
    data.append(row[0].value)
</code>```

This prints only part. I want to bring the whole. Excel files are only stored in column A.

答:
This will give you a list of lists for each row:

import openpyxl

workbook = openpyxl.load_workbook(filename='Financial Sample.xlsx')
sheet = workbook.active

data = [[cell.value for cell in row] for row in sheet.rows]

print(data)
```

<p>Output (some sample data):</p>

```[['Segment', 'Country', 'Product', 'Discount Band', 'Units Sold', 'Manufacturing Price', 'Sale Price', 'Gross Sales', 'Discounts', ' Sales', 'COGS', 'Profit', 'Date', 'Month Number', 'Month Name', 'Year'],
 ['Channel Partners', 'Canada', 'Montana', 'None', 2518, 5, 12, 30216, 0, 30216, 7554, 22662, datetime.datetime(2014, 6, 1, 0, 0), 6, 'June', '2014']]
```

ContentLicense: CC BY-SA 4.0

Body:
data=[]
workbook=openpyxl.load_workbook(filename='crawling.xlsx')
sheet=workbook.active
for row in sheet.rows:
    data.append(row[0].value)

This prints only part. I want to bring the whole. Excel files are only stored in column A.

Body:
This will give you a list of lists for each row:

import openpyxl

workbook = openpyxl.load_workbook(filename='Financial Sample.xlsx')
sheet = workbook.active

data = [[cell.value for cell in row] for row in sheet.rows]

print(data)

Output (some sample data):

[['Segment', 'Country', 'Product', 'Discount Band', 'Units Sold', 'Manufacturing Price', 'Sale Price', 'Gross Sales', 'Discounts', ' Sales', 'COGS', 'Profit', 'Date', 'Month Number', 'Month Name', 'Year'],
 ['Channel Partners', 'Canada', 'Montana', 'None', 2518, 5, 12, 30216, 0, 30216, 7554, 22662, datetime.datetime(2014, 6, 1, 0, 0), 6, 'June', '2014']]


问:
Need explanation for C++ code
```<code>namespace Stack {
  struct Rep; // definition of stack layout is elsewhere
  typedef Rep& stack;
  stack create(); // make a new stack
  void destroy(stack s); // delete s
  void push(stack s, char c); // push c onto s
  char pop(stack s); // pop s
}
</code>```

This is in the "A Tour of C++" Unit, before even the basic material like functions, loops, and fundamental datatypes...I don't know if/how I'm supposed to understand this code or not.

Anyway, can someone please explain it? It's supposed to be part of an explanation on how to "define a stack manager with an interface"

For starters, I specifically don't know what "typedef Rep& stack" means.

答:
This is a way of showing C coders how C++ Objects formed; namespace Stack { struct Rep; } will give you a Stack::Rep item/object which is similar to doing class Stack { struct Rep;}.  If you are not familiar with C and namespaces then it does seem an odd example to have at the start of the book.

Anyway that is my best guess as when I first saw this after using C for a while I realised "Oh, that is how objects sort of came about.  They are namespaces with some extra fun added in to the compiler!"

ContentLicense: CC BY-SA 3.0

Title: Need explanation for C++ code

Body:
namespace Stack {
  struct Rep; // definition of stack layout is elsewhere
  typedef Rep& stack;
  stack create(); // make a new stack
  void destroy(stack s); // delete s
  void push(stack s, char c); // push c onto s
  char pop(stack s); // pop s
}

This is in the "A Tour of C++" Unit, before even the basic material like functions, loops, and fundamental datatypes...I don't know if/how I'm supposed to understand this code or not.

Anyway, can someone please explain it? It's supposed to be part of an explanation on how to "define a stack manager with an interface"

For starters, I specifically don't know what "typedef Rep& stack" means.


问:
How do I read from the terminal in Python?
I just want to use os.system("dir") and also be able to save the text outputted to a variable.  I tried using sys.stdout.read() but running sys.stdout.readable() returns False.  Do you know how I can read from the terminal?

答:
If you want to read just go with

```<code>x = input()
</code>```

This reads a one line from the terminal. x is a string by default, but you can cast it, say to int, like so

```<code>x = int(x)
</code>```

Body:
If you want to read just go with

x = input()

This reads a one line from the terminal. x is a string by default, but you can cast it, say to int, like so

x = int(x)


问:
How to browse a csv in Python by intervals of letters at the beginning of lines
I have a csv that contains a lot of data. When I launch a webscraping, I receive a:

```<code>TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
</code>```

In order to limit the amount of data to be processed for webscraping, I would like to divide the following script into several scripts, each browsing intervals of the csv file:

```<code># Get the data from the csv containing pmid list by author :
with open("D:/Nancy/Pèse-Savants/Excercice Covid-19/Exercice 3/pmid_par_auteur.csv",'r', encoding='utf-8') as f:
    # Sseperate author's list from pmid's list into 2 columns :
    with open ("pmid_par_auteur_uniformise.csv", "w", encoding='utf-8') as fu:
        csv_f = csv.reader(f, delimiter = ';')
        for ligne in csv_f:
            fu.write(ligne[0] + '\n')

auteur_pmid_doi = []

# Clean up encoded data in 'utf-8'
with open("pmid_par_auteur_uniformise.csv",encoding='utf-8') as fu:
    csv_fu = csv.reader(fu)

for ligne in csv_fu:
    ligne[1] = ligne[1].replace("'", " ")
    ligne[1] = ligne[1].replace("[", " ")
    ligne[1] = ligne[1].replace("]", " ")
    ligne[1] = ligne[1].split(" , ")

# Get DOI for each pmid for each author that wrote on Covid-19
    pmid_doi = []

    for pmid in ligne[1]:

        try :
            handle = Entrez.esummary(db="pubmed", id=pmid)
            record = Entrez.read(handle)
            record = record[0]['DOI']
        except IndexError :
            print ('Missing DOI')
        except KeyError :
            print ('Missing DOI')

        else :
            pmid_doi.append([pmid, record])

#handles are a finite resource, I close it in order to avoid exhausting the handle supply with a large dataset.
        handle.close()

# Delete temporary variables to free some space in the RAM:
    auteur_pmid_doi.append([ligne[0], pmid_doi])
    del (ligne[1])
    del (handle)
    del (record)
    del (pmid_doi)

auteur_pmid_doi
</code>```

Each script would run through a data interval ranging like this:

From the first line starting with letter A to the last line starting with the letter E.

From the first line starting with letter F, to the last line starting with the letter J.

From the first line starting with the letter k to the last line starting with the letter O.

And so on up to “z”.

How do you browse the lines of a csv through these types of intervals?

I add the link to my csv and thank you in advance for your help.

pmid_par_auteur_uniformise.csv

Body:
I have a csv that contains a lot of data. When I launch a webscraping, I receive a:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

In order to limit the amount of data to be processed for webscraping, I would like to divide the following script into several scripts, each browsing intervals of the csv file:

# Get the data from the csv containing pmid list by author :
with open("D:/Nancy/Pèse-Savants/Excercice Covid-19/Exercice 3/pmid_par_auteur.csv",'r', encoding='utf-8') as f:
    # Sseperate author's list from pmid's list into 2 columns :
    with open ("pmid_par_auteur_uniformise.csv", "w", encoding='utf-8') as fu:
        csv_f = csv.reader(f, delimiter = ';')
        for ligne in csv_f:
            fu.write(ligne[0] + '\n')

auteur_pmid_doi = []

# Clean up encoded data in 'utf-8'
with open("pmid_par_auteur_uniformise.csv",encoding='utf-8') as fu:
    csv_fu = csv.reader(fu)

for ligne in csv_fu:
    ligne[1] = ligne[1].replace("'", " ")
    ligne[1] = ligne[1].replace("[", " ")
    ligne[1] = ligne[1].replace("]", " ")
    ligne[1] = ligne[1].split(" , ")

# Get DOI for each pmid for each author that wrote on Covid-19
    pmid_doi = []

    for pmid in ligne[1]:

        try :
            handle = Entrez.esummary(db="pubmed", id=pmid)
            record = Entrez.read(handle)
            record = record[0]['DOI']
        except IndexError :
            print ('Missing DOI')
        except KeyError :
            print ('Missing DOI')

        else :
            pmid_doi.append([pmid, record])

#handles are a finite resource, I close it in order to avoid exhausting the handle supply with a large dataset.
        handle.close()

# Delete temporary variables to free some space in the RAM:
    auteur_pmid_doi.append([ligne[0], pmid_doi])
    del (ligne[1])
    del (handle)
    del (record)
    del (pmid_doi)

auteur_pmid_doi

Each script would run through a data interval ranging like this:

From the first line starting with letter A to the last line starting with the letter E.

From the first line starting with letter F, to the last line starting with the letter J.

From the first line starting with the letter k to the last line starting with the letter O.

And so on up to “z”.

How do you browse the lines of a csv through these types of intervals?

I add the link to my csv and thank you in advance for your help.

pmid_par_auteur_uniformise.csv

Body:
Assuming the file is sorted, the following will:

group rows into their starting letter.

collect rows from five groups at a time.

call process() with the rows.

sample process() will:

convert the pmids string to a list of strings

count the number of rows

print the first and last row of the group.

code:

import csv
import itertools
import ast

def process(rows):
    rows = [[name,ast.literal_eval(pmids)] for name,pmids in rows]
    print(f' -> {len(rows)} row(s)')
    print(f'    first row: {rows[0]}')
    print(f'    last row:  {rows[-1]}')

with open('pmid_par_auteur_uniformise.csv',encoding='utf-8-sig',newline='') as f:
    r = csv.reader(f)
    rows = []
    for i,(key,group) in enumerate(itertools.groupby(r,key=lambda x: x[0][0])):
        rows.extend(list(group))
        print(key,end='')
        if i % 5 == 4:
            process(rows)
            rows = []
    if rows:
        process(rows)

Output:

ABCDE -> 3124 row(s)
    first row: ['A Aljabali Alaa A', ['32397911']]
    last row:  ['Eğrilmez Sait', ['32366061', '32328919', '32099313', '31486610', '31245968', '31045617', '30202612', '29697353', '29109898', '28761568', '28405481', '28169512', '27849319', '27758983', '27447356', '27028890', '26887566', '26801653', '26654385', '26558208', '26401170', '26208680', '26159181', '25955827', '25247377', '25111119', '24621171', '24367973', '24322806', '23970986', '23684361', '23205897', '23061415', '21598429', '21453232', '21414053', '21353409', '21332970', '20490802', '20385953', '20164799', '19958116', '26649479', '19237782', '18404071', '18209647', '18039348', '17641704', '15696768', '15305561', '15177606', '15177597', '15050248', '12908533', '12780405', '12752051', '12662987']]
FGHIJ -> 2087 row(s)
    first row: ['Fabbricatore Davide', ['32383763', '31898206', '31564087', '29754460', '29460403', '29252600', '28775965']]
    last row:  ['Jüni Peter', ['32450456', '32396180', '32385067', '32385063', '32333878', '32294317', '32293511', '32241376', '32215640', '32199780', '32139280', '32139222', '32006758', '32006156', '31920002', '31857278', '31857277', '31854112', '31851302', '31845894', '31841136', '31707794', '31696762', '31693078', '31672177', '31648781', '31589276', '31570258', '31537275', '31525083', '31497854', '31488373', '31488372', '31476244', '31462531', '31434508', '31410968', '31397487', '31379378', '31368907', '31329852', '31269364', '31217143', '31204678', '31197439', '31164366', '31132298', '31084961', '31056295', '30975683', '30888959', '30852547', '30846254', '30833323', '30789921', '30703644', '30689825', '30667361', '30601734', '30596995', '30592349', '30566213', '30560696', '30424891', '30356345', '30354650', '30354532', '30347031', '30291678', '30215374', '30182362', '30170848', '30166073', '30165632', '30165437', '30165435', '30153988', '30146969', '30107514', '30044478', '29992264', '29916872', '29912740', '29885826', '29850808', '29794879', '29786535', '29785878', '29742109', '29628287', '29606865', '29487111', '29478826', '29467161', '29277234', '29251754', '29228059', '29205157', '29162610', '29155984', '29130845', '29113968', '29097450', '29045581', '29038228', '29020259', '28967416', '28948934', '28886622', '28850362', '28827257', '28796809', '28790165', '28781251', '28742627', '28732814', '28699595', '28671552', '28611089', '28601820', '28566364', '28536005', '28528767', '28472484', '28430920', '28425755', '28330794', '28329389', '28253938', '28213601', '28213600', '28185702', '28079554', '28067197', '28029055', '28027351', '28017369', '28003290', '27998831', '27923461', '27884241', '27753599', '27733354', '27677503', '27665852', '27578808', '27497359', '27479866', '27478115', '27437661', '27389906', '27372195', '27318845', '27296200', '27289296', '27252878', '27179724', '27125947', '27078262', '27033859', '31997951', '26997557', '26979080', '26916479', '26896474', '26823484', '26762519', '26741741', '26700531', '26690319', '26655339', '26649651', '26606735', '26585615', '26490760', '26453687', '26428025', '26408014', '26376691', '26373562', '26352574', '26334160', '26324049', '26210282', '26208006', '26205445', '26196758', '26142466', '26071600', '26043895', '26040806', '26010634', '26007299', '25979551', '25934823', '25910501', '25875821', '25858975', '25794671', '25794517', '25791214', '25634905', '25623431', '25572026', '25551539', '25546177', '25529190', '25524605', '25495124', '25494429', '25489846', '25433627', '25423953', '25416325', '25330508', '25229835', '25208215', '25189359', '25187201', '25184244', '25182248', '25176289', '25173601', '25173535', '25173339', '25169183', '25163691', '25112661', '25042419', '25042271', '25011716', '24958760', '24958153', '24919052', '24882698', '24847017', '24755380', '24738641', '24711124', '24694729', '24682843', '24676282', '24631113', '24602961', '24552862', '24531331', '24429160', '24332419', '24206920', '24132187', '24064474', '24064377', '24039795', '23993323', '23968698', '23946263', '23909727', '23822782', '23793972', '23759706', '23747228', '23723742', '23702009', '23514285', '23487519', '23386662', '23370065', '23339812', '23277909', '23169986', '23152242', '23045205', '23008508', '22995882', '22945832', '22924638', '22922416', '22910755', '22868835', '22846347', '22759453', '22739992', '22726632', '22711083', '22645184', '22625186', '22607867', '22580250', '22456025', '22447805', '22440496', '22362513', '22361598', '22319063', '22302840', '22301368', '22285579', '22238228', '22093210', '22078420', '22075451', '22056618', '22027687', '22008217', '21959221', '21931648', '21930465', '21904996', '21878462', '21851904', '21768536', '21700254', '21646500', '21641358', '21632168', '21596229', '21396782', '21385807', '21362706', '21356042', '26063638', '21330239', '21296599', '21224324', '21205944', '21161860', '21042932', '20884434', '20870808', '20853471', '20847017', '20807617', '20639294', '20633818', '26061467', '20562074', '20506333', '20464751', '20461793', '20298923', '20152241', '20152233', '20142179', '20091539', '19950329', '19930626', '19889649', '19821404', '19821403', '19821302', '19821296', '19819375', '19778775', '19736281', '19736154', '19679616', '19620501', '19370423', '19284063', '19204314', '19074491', '19036745', '18804739', '18765162', '18757996', '18534034', '18512273', '18502079', '18414453', '18316340', '18272504', '18050181', '17968921', '17903638', '17869634', '17868802', '17726091', '17707588', '17696267', '17606174', '17606172', '17602184', '17438317', '17321312', '16979535', '16824829', '16717169', '16704569', '16255025', '16125589', '16105989', '15947376', '15911545', '15897534', '15649954', '15641050', '15582059', '15513969', '15485938', '15122753', '15087341', '14960422', '12912727', '12814907', '12654410', '12574052', '12456259', '12435252', '12111917', '12039807', '12038917', '11914306']]
KLMNO -> 3147 row(s)
    first row: ['Kaakinen Markus', ['32320402', '32306232', '32301734', '31895044', '31815340', '31761930', '30465150', '30176935', '29892700', '29623505', '29048938', '27441787', '27094352', '26620915', '26563678']]
    last row:  ['Ozog David M', ['32452977', '32446829', '32442698', '32291807', '32246972', '32224709', '31820478', '31797796', '31743247', '31592926', '31567612', '31449081', '31335426', '31335419', '31268498', '30528311', '30528310', '30322300', '30235390', '29381548', '28975212', '28658462', '28522039', '28522038', '27984327', '27336945', '27051812', '26845540', '26547045', '26504503', '26458039', '25946625', '25738444', '24891062', '24664987', '24196328', '23157724', '23069917', '22269028', '22093099', '22055283', '21931055', '21864935', '21518098', '21457398', '20855672', '20384757', '20227579', '17254036', '16918570']]
PQRST -> 2954 row(s)
    first row: ['Paakkari Leena', ['32438595', '32302535', '31654998', '31410386', '31297559', '30753409', '30578457', '30412226', '29510702', '28673131', '27655781', '26630180', '24609436']]
    last row:  ['Tánczos Krisztián', ['32453702', '29953455', '29264668', '27597981', '27288610', '26543848', '25608924', '24818123', '24457113', '24012232']]
UVWXY -> 1509 row(s)
    first row: ['Ubaldi Filippo M', ['32404170', '32354663', '32038484', '31824427', '31756248', '31403619', '31174207', '30848112', '30739329', '28895679', '28362681', '27827818', '27310263', '26921622', '26728489', '26531067', '25985139', '24884585', '21665542', '16580372']]
    last row:  ['Yılmaz Aydın', ['32299200', '32029697', '31799089', '31615316', '31130132', '30683026', '30582673', '29151304', '29135403', '29052061', '28940588', '28653494', '28621292', '28393719', '27704696', '27481085', '27442525', '27412127', '26885104', '26556895', '26523900', '26482979', '26422878', '26374581', '26281327', '26257957', '26107228', '25619495', '25492957', '25492815', '24976998', '24814084', '24506753', '23949189', '23431310', '23377781', '23030749', '22609980', '22320975', '22087531', '22019748', '21851414', '21334917', '21038140', '20981186', '20954282', '20689268', '20517728', '20332658', '19803278', '17845896', '16650973', '16304288', '16217989', '15143428', '14971870']]
ZdvÁÇ -> 741 row(s)
    first row: ['Zabetakis Ioannis', ['32438620', '32340775', '32270798', '32224958', '31816871', '31540159', '31137500', '30721934', '30669323', '30619728', '30381909', '30319088', '29882848', '29757226', '29494487', '29135918', '28714908', '28119955', '27109548', '24973582', '24735421', '24128590', '24084786', '23957417', '23480708', '23433838', '25212344', '22087726', '26047447', '16390205']]
    last row:  ['Çinier Göksel', ['32462219', '32406873', '32338313', '32250347', '32222434', '32147660', '32147654', '32035356', '31846583', '31764010', '31707766', '31670716', '31582673', '31542896', '31483310', '31339201', '31204510', '31139269', '31038781', '30961362', '30928819', '30815636', '30808220', '30694809', '30230925', '30174880', '30149941', '30075884', '30024391', '30022507', '29894304', '29848928', '29523425', '29487682', '29451310', '29339686', '29191504', '28971172', '28898454', '28864320', '28838153', '28595215', '28595209', '28592959', '28424447', '28401800', '28169085', '27641906', '27608320', '27581673', '27414730', '27341666', '26946973', '26778640', '26295613']]
ÖØÜĆČ -> 12 row(s)
    first row: ['Özdemir Vural', ['32319847', '32316827', '32223589', '32105560', '32027574', '31990612', '31855503', '31794335', '31794294', '31199695', '31094658', '31066623', '30789303', '30707659', '30362880', '30281399', '30260734', '30036157', '30004300', '29791250', '29432059', '29431577', '29293405', '29083982', '29064337', '28655746', '28622116', '28253085', '27726641', '27631187', '27310474', '27211534', '26793622', '26785082', '26684591', '26645377', '26484977', '26430925', '26345196', '26270647', '26161545', '26066837', '26061584', '25970399', '25748435', '25656538', '25353263', '25000304', '24955641', '24795761', '24795460', '24766116', '24730382', '24649998', '24521341', '24456465', '24456464', '26120345', '27447251', '24048056', '27442201', '23765483', '23574338', '23531886', '23301640', '23258262', '23249198', '23249197', '23249193', '23249192', '23194449', '22987569', '22545073', '22523528', '22401659', '22279516', '22279515', '22220951', '22198458', '21848419', '21848418', '21490881', '21476845', '21400375', '21399751', '20977184', '20808949', '20547595', '20526970', '20455752', '19882466', '19470561', '19290811', '19290807', '19214141', '19207026', '19040373', '19040370', '18708948', '18570104', '18480690', '18266561', '18075468', '17963681', '17924827', '17716237', '17559347', '17481382', '17481379', '17474081', '17439540', '17429316', '17286540', '17224711', '17184207', '17093467', '16900136', '16544144', '16433578', '15738749', '14965233', '14582457', '12054061', '11864722']]
    last row:  ['Čivljak Rok', ['32426118', '32118371', '31661701', '30801727', '29164144', '27625226', '26638539', '26538030', '26494527', '26012149', '25801665', '25634680', '25274934', '24392752', '24192278', '23941014', '21294322', '23120878', '18773823', '15936084', '15330131']]
İŚŞŠ -> 10 row(s)
    first row: ['İrkeç Murat', ['32366061', '32287143', '31844978', '31232743', '30605936', '30471351', '29944505', '29554816', '29196953', '29135602', '28598963', '28553957', '28390092', '27874294', '27790127', '27775457', '27467041', '27513901', '27452505', '27055218', '26257227', '26187885', '26035419', '25811727', '25686056', '25642816', '25603441', '25370397', '25264994', '25203662', '25119963', '25069074', '25069002', '24967185', '24803156', '24790882', '24774884', '24767236', '24646901', '24627252', '24401152', '24216677', '24145558', '23806084', '23730902', '23635854', '23564611', '23561604', '23378724', '23377585', '23323578', '23084388', '22885886', '22799438', '22415150', '21575121', '21174000', '20813741', '20595897', '20544681', '20299976', '19878104', '19825835', '19681791', '19618994', '19491966', '19396777', '19158563', '19085375', '18700919', '18675411', '18580271', '18524193', '18471650', '18414106', '18352875', '18216575', '18083592', '17760635', '17522853', '17519661', '17413955', '17300569', '17198020', '17102682', '17068451', '16849641', '16280977', '16075220', '15968156', '15068429', '14746595', '14746583', '14566648', '14533030', '12791215', '12789598', '12789579', '12738551', '12695716', '12648019', '12427231', '12072719', '12065849', '12027104', '11821216', '11820668']]
    last row:  ['Šín Robin', ['32434337', '32304370', '31974532', '31971245']]


问:
Connected to the same router = Same ip and port?
Three computers connected to the same router will have the same public IP. If these computers send a request to my server, will they all have the same PORT as well or are there exceptions?

EDIT: When I get requests from the browser, the PORT is different for each connection it creates. Does the browser client just pick a random port that is available on the router?

答:
Three computers connected to the same router will have the same public IP.

Correct, from the server's perspective, not from the client's perspective.

If these computers send a request to my server, will they all have the same PORT as well or are there exceptions?

No, they will not have the same Port on the router (they may on each client PC, though).

A TCP connection is uniquely identified by the tuple {protocol, local-ip, local-port, remote-ip, remote-port}.  So, when multiple TCP connections have the same {remote-ip, remote-port} (IOW, when multiple clients are connected to the same server), then each {local-ip, local-port} must be unique.  And vice versa, when multiple TCP connections have the same {local-ip, local-port} (IOW, when a client connects to multiple servers), then each {remote-ip, remote-port} must be unique.

When passing through a router, each TCP connection as seen on the client side will be {TCP, lan-ip, lan-port, server-ip, server-port}, while on the server side each connection will be seen as {TCP, listen-ip, listen-port, client-ip, client-port}, where {client-ip, client-port} will be the router's {public-ip, public-port}, so each {public-ip, public-port} must be unique.

So, multiple clients connecting to the same server through a router simply cannot use the same outgoing port on the router, otherwise the server would not be able to differentiate between the connections.

When I get requests from the browser, the PORT is different for each connection it creates.

Correct.

Does the browser client just pick a random port that is available on the router?

No, nor does the browser care that a router is present.  The browser creates a local socket endpoint and binds it to an available {local-ip, local-port} and then uses it to connect to the server's {server-ip, server-port}.  The packets go to the OS, the OS sends them to the router, the router opens its own available {public-ip, public-port} for each new connection and then forwards those packets to the server.  When the server sends packets back, the router will receive those packets on its public NIC, forward them to the appropriate client OS, which will pass them to the appropriate socket endpoint.

```
 -------------
| Client PC A |
 -------------
 {tcp, client-lan-ip, client-lan-port, server-ip, server-port}

   /|\
    |
   \|/

 {tcp, router-lan-ip, router-lan-port, client-lan-ip, client-lan-port}
 --------
| Router |
 --------
 {tcp, router-public-ip, router-public-port, server-ip, server-port}

   /|\
    |
   \|/

{tcp, listen-ip, listen-port, router-public-ip, router-public-port}
 --------
| Server |
 --------
```

Body:
Three computers connected to the same router will have the same public IP.

Correct, from the server's perspective, not from the client's perspective.

If these computers send a request to my server, will they all have the same PORT as well or are there exceptions?

No, they will not have the same Port on the router (they may on each client PC, though).

A TCP connection is uniquely identified by the tuple {protocol, local-ip, local-port, remote-ip, remote-port}.  So, when multiple TCP connections have the same {remote-ip, remote-port} (IOW, when multiple clients are connected to the same server), then each {local-ip, local-port} must be unique.  And vice versa, when multiple TCP connections have the same {local-ip, local-port} (IOW, when a client connects to multiple servers), then each {remote-ip, remote-port} must be unique.

When passing through a router, each TCP connection as seen on the client side will be {TCP, lan-ip, lan-port, server-ip, server-port}, while on the server side each connection will be seen as {TCP, listen-ip, listen-port, client-ip, client-port}, where {client-ip, client-port} will be the router's {public-ip, public-port}, so each {public-ip, public-port} must be unique.

So, multiple clients connecting to the same server through a router simply cannot use the same outgoing port on the router, otherwise the server would not be able to differentiate between the connections.

When I get requests from the browser, the PORT is different for each connection it creates.

Correct.

Does the browser client just pick a random port that is available on the router?

No, nor does the browser care that a router is present.  The browser creates a local socket endpoint and binds it to an available {local-ip, local-port} and then uses it to connect to the server's {server-ip, server-port}.  The packets go to the OS, the OS sends them to the router, the router opens its own available {public-ip, public-port} for each new connection and then forwards those packets to the server.  When the server sends packets back, the router will receive those packets on its public NIC, forward them to the appropriate client OS, which will pass them to the appropriate socket endpoint.

 -------------
| Client PC A |
 -------------
 {tcp, client-lan-ip, client-lan-port, server-ip, server-port}

   /|\
    |
   \|/

 {tcp, router-lan-ip, router-lan-port, client-lan-ip, client-lan-port}
 --------
| Router |
 --------
 {tcp, router-public-ip, router-public-port, server-ip, server-port}

   /|\
    |
   \|/

{tcp, listen-ip, listen-port, router-public-ip, router-public-port}
 --------
| Server |
 --------


问:
How to send a message outside the controller?
I need to send information to the connected clients outside the HUB.

Here my class :

```<code>public static class Notification{
  public static void SendMessage(){
    //... Do some stuff
    MyHub.Clients.All.SendAsync("sendInfo");
  }
}
</code>```

How to instantiate HUB?

答:
As far as I know, you could use IHubContext service to send the service message outside the hub.

If you have register the service inside the ConfigureServices in startup.cs, then you could  access an instance of IHubContext via dependency injection.

```<code>        services.AddSignalR();
</code>```

E.g Inject an instance of IHubContext in a controller.

```<code>public class HomeController : Controller
{
    private readonly IHubContext<NotificationHub> _hubContext;

    public HomeController(IHubContext<NotificationHub> hubContext)
    {
        _hubContext = hubContext;
    }
}
</code>```

Now, with access to an instance of IHubContext, you can call hub methods as if you were in the hub itself.

```<code>public async Task<IActionResult> Index()
{
    await _hubContext.Clients.All.SendAsync("Notify", $"Home page loaded at: {DateTime.Now}");
    return View();
}
</code>```

More details ,you could refer to this article.

Body:
I need to send information to the connected clients outside the HUB.

Here my class :

public static class Notification{
  public static void SendMessage(){
    //... Do some stuff
    MyHub.Clients.All.SendAsync("sendInfo");
  }
}

How to instantiate HUB?

Body:
As far as I know, you could use IHubContext service to send the service message outside the hub.

If you have register the service inside the ConfigureServices in startup.cs, then you could  access an instance of IHubContext via dependency injection.

        services.AddSignalR();

E.g Inject an instance of IHubContext in a controller.

public class HomeController : Controller
{
    private readonly IHubContext<NotificationHub> _hubContext;

    public HomeController(IHubContext<NotificationHub> hubContext)
    {
        _hubContext = hubContext;
    }
}

Now, with access to an instance of IHubContext, you can call hub methods as if you were in the hub itself.

public async Task<IActionResult> Index()
{
    await _hubContext.Clients.All.SendAsync("Notify", $"Home page loaded at: {DateTime.Now}");
    return View();
}

More details ,you could refer to this article.


问:
Should i have different versions for a paragraph
Should i have different versions for the paragraph for a desktop view and a mobile view?
If on desktop, in the css mobile would be display:none and then vice versa for desktop

```<code>  <p className="Desktop">
          "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
          <br />eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
          <br />minim veniam, quis nostrud exercitation ullamco laboris nisi ut
          <br />aliquip ex ea commodo consequat. Duis aute irure dolor in
       </p>
</code>```

```<code> <p className="mobile">
          "Lorem ipsum dolor sit amet, <br />
          consectetur adipiscing elit, sed do
          <br />
          eiusmod tempor incididunt ut labore <br />
          et dolore magna aliqua. Ut enim ad
          <br />
          minim veniam, quis nostrud <br />
          exercitation ullamco laboris nisi ut
          <br />
          aliquip ex ea commodo consequat. <br /> Duis aute irure dolor in
        </p>
</code>```

答:
Your question is about the flexibility.

First of all, you should use class, instead of className.

Then, there is no need to make two different paragraphs. It'd take too much time. Modern browsers define p element automatically 100% of the window. So there is no need to put
 tag for each line.

In addition to this, if you want to make some changes for only mobile or desktop, you can use @media like this:

```<code>@media (max-width: 767px){
    .custom-p{
        /*Here is the mobile codes*/
    }
}
</code>```

But on your question, you don't even need this. Just write the paragraph.

Body:
Should i have different versions for the paragraph for a desktop view and a mobile view?
If on desktop, in the css mobile would be display:none and then vice versa for desktop

          "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do

eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad

minim veniam, quis nostrud exercitation ullamco laboris nisi ut

aliquip ex ea commodo consequat. Duis aute irure dolor in

          "Lorem ipsum dolor sit amet,

          consectetur adipiscing elit, sed do

          eiusmod tempor incididunt ut labore

          et dolore magna aliqua. Ut enim ad

          minim veniam, quis nostrud

          exercitation ullamco laboris nisi ut

          aliquip ex ea commodo consequat.
 Duis aute irure dolor in

Body:
Your question is about the flexibility.

First of all, you should use class, instead of className.

Then, there is no need to make two different paragraphs. It'd take too much time. Modern browsers define p element automatically 100% of the window. So there is no need to put <br> tag for each line.

In addition to this, if you want to make some changes for only mobile or desktop, you can use @media like this:

@media (max-width: 767px){
    .custom-p{
        /*Here is the mobile codes*/
    }
}

But on your question, you don't even need this. Just write the paragraph.


问:
Is the emmeans (R) intercept-only function broken?
I've noticed that emmeans (in R) isn't working for an intercept-only estimate after the latest update.

Reproducible example:

```<code>test=lm(mpg~1,mtcars)
library(emmeans)
emmeans::emmeans(test,~1)
</code>```

The output on 2 of my machines (windows and Linux) is:

```<code>> emmeans::emmeans(test,~1)
Error in `[[<-.data.frame`(`*tmp*`, ".wgt.", value = 2) :
  replacement has 1 row, data has 0
</code>```

Is this a known issue, or have I messed up my system somehow?
This used to work I believe.

It does work if you include a variable:

```<code>test2=lm(mpg~as.factor(cyl),mtcars)
emmeans(test2,~cyl)
</code>```

Thanks very much for the help in advance.

答:
It turns out that the fix for issue #197 -- and incorporated in CRAN version 1.47 -- created the issue (#206) that we see here. I think I have them both fixed now:

require(emmeans)
## Loading required package: emmeans

#206...
warp.lm <- lm(breaks ~ wool * tension, data = warpbreaks)

emmeans(warp.lm, "1")
##  1       emmean   SE df lower.CL upper.CL
##  overall   28.1 1.49 48     25.2     31.1
##
## Results are averaged over the levels of: wool, tension
## Confidence level used: 0.95

emmeans(warp.lm, "1", by = "wool")
## wool = A:
##  1       emmean   SE df lower.CL upper.CL
##  overall   31.0 2.11 48     26.8     35.3
##
## wool = B:
##  1       emmean   SE df lower.CL upper.CL
##  overall   25.3 2.11 48     21.0     29.5
##
## Results are averaged over the levels of: tension
## Confidence level used: 0.95

#197...
model <- lm(Sepal.Length ~ poly(Petal.Length,2), data = iris)

emtrends(model, ~ 1, "Petal.Length", max.degree = 2)
## degree = linear:
##  1       Petal.Length.trend     SE  df lower.CL upper.CL
##  overall             0.4474 0.0180 147   0.4119    0.483
##
## degree = quadratic:
##  1       Petal.Length.trend     SE  df lower.CL upper.CL
##  overall             0.0815 0.0132 147   0.0554    0.108
##
## Confidence level used: 0.95
```

<p><sup>Created on 2020-06-01 by the reprex package (v0.3.0)</sup></p>

<p>Users who need this now can install from github via</p>

<pre class="lang-r prettyprint-override"><code>remotes::install_github("rvlenth/emmeans")
</code>```

Body:
I've noticed that emmeans (in R) isn't working for an intercept-only estimate after the latest update.

Reproducible example:

test=lm(mpg~1,mtcars)
library(emmeans)
emmeans::emmeans(test,~1)

The output on 2 of my machines (windows and Linux) is:

> emmeans::emmeans(test,~1)
Error in `[[<-.data.frame`(`*tmp*`, ".wgt.", value = 2) :
  replacement has 1 row, data has 0

Is this a known issue, or have I messed up my system somehow?
This used to work I believe.

It does work if you include a variable:

test2=lm(mpg~as.factor(cyl),mtcars)
emmeans(test2,~cyl)

Thanks very much for the help in advance.

Body:
It turns out that the fix for issue #197 -- and incorporated in CRAN version 1.47 -- created the issue (#206) that we see here. I think I have them both fixed now:

require(emmeans)
## Loading required package: emmeans

#206...
warp.lm <- lm(breaks ~ wool * tension, data = warpbreaks)

emmeans(warp.lm, "1")
##  1       emmean   SE df lower.CL upper.CL
##  overall   28.1 1.49 48     25.2     31.1
##
## Results are averaged over the levels of: wool, tension
## Confidence level used: 0.95

emmeans(warp.lm, "1", by = "wool")
## wool = A:
##  1       emmean   SE df lower.CL upper.CL
##  overall   31.0 2.11 48     26.8     35.3
##
## wool = B:
##  1       emmean   SE df lower.CL upper.CL
##  overall   25.3 2.11 48     21.0     29.5
##
## Results are averaged over the levels of: tension
## Confidence level used: 0.95

#197...
model <- lm(Sepal.Length ~ poly(Petal.Length,2), data = iris)

emtrends(model, ~ 1, "Petal.Length", max.degree = 2)
## degree = linear:
##  1       Petal.Length.trend     SE  df lower.CL upper.CL
##  overall             0.4474 0.0180 147   0.4119    0.483
##
## degree = quadratic:
##  1       Petal.Length.trend     SE  df lower.CL upper.CL
##  overall             0.0815 0.0132 147   0.0554    0.108
##
## Confidence level used: 0.95

Created on 2020-06-01 by the reprex package (v0.3.0)

Users who need this now can install from github via

remotes::install_github("rvlenth/emmeans")


问:
Read Parquet file stored in S3 with AWS Lambda (Python 3)
I am trying to load, process and write Parquet files in S3 with AWS Lambda. My testing / deployment process is:

https://github.com/lambci/docker-lambda as a container to mock the Amazon environment, because of the native libraries that need to be installed (numpy amongst others).

This procedure to generate a zip file: http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html#with-s3-example-deployment-pkg-python

Add a test python function to the zip, send it to S3, update the lambda and test it

It seems that there are two possible approaches, which both work locally to the docker container:

fastparquet with s3fs: Unfortunately the unzipped size of the package is bigger than 256MB and therefore I can't update the Lambda code with it.

pyarrow with s3fs: I followed https://github.com/apache/arrow/pull/916 and when executed with the lambda function I get either:

If I prefix the URI with S3 or S3N (as in the code example): In the Lambda environment OSError: Passed non-file path: s3://mybucket/path/to/myfile in pyarrow/parquet.py, line 848. Locally I get IndexError: list index out of range in pyarrow/parquet.py, line 714

If I don't prefix the URI with S3 or S3N: It works locally (I can read the parquet data). In the Lambda environment, I get the same OSError: Passed non-file path: s3://mybucket/path/to/myfile in pyarrow/parquet.py, line 848.

My questions are  :

why do I get a different result in my docker container than I do in the Lambda environment?

what is the proper way to give the URI?

is there an accepted way to read Parquet files in S3 through AWS Lambda?

Thanks!

答:
One can also achieve this through the AWS sam cli and Docker (we'll explain this requirement later).

1.Create a directory and initialize sam

```<code>mkdir some_module_layer
cd some_module_layer
sam init
</code>```

by typing the last command a series of three question would be prompted. One could choose the following series of answers (I'm considering working under Python3.7, but other options are possible).

1 - AWS Quick  Start Templates

8 - Python 3.7

Project name [sam-app]: some_module_layer

1 - Hello World Example

2. Modify requirements.txt file

```<code>cd some_module_layer
vim hello_world/requirements.txt
</code>```

this will open requirements.txt file on vim, on Windows you could type instead code hello_world/requirements.txt to edit the file on Visual Studio Code.

3. Add pyarrow to requirements.txt

Alongside pyarrow, it will work to include additionnaly pandas and s3fs. In this case including pandas will avoid it to not recognize pyarrow as an engine to read parquet files.

```<code>pandas
pyarrow
s3fs
</code>```

4. Build with a container

Docker is required to use the option --use-container when running the sam build command. If it's the first time, it will pull the lambci/lambda:build-python3.7 Docker  image.

```<code>sam build --use-container
rm .aws-sam/build/HelloWorldFunction/app.py
rm .aws-sam/build/HelloWorldFunction/__init__.py
rm .aws-sam/build/HelloWorldFunction/requirements.txt
</code>```

notice that we're keeping only the python libraries.

5. Zip files

```<code>cp -r .aws-sam/build/HelloWorldFunction/ python/
zip -r some_module_layer.zip python/
</code>```

On Windows, it would work to run  Compress-Archive python/ some_module_layer.zip.

6. Upload zip file to AWS

The following link is useful for this.

Body:
One can also achieve this through the AWS sam cli and Docker (we'll explain this requirement later).

1.Create a directory and initialize sam

mkdir some_module_layer
cd some_module_layer
sam init

by typing the last command a series of three question would be prompted. One could choose the following series of answers (I'm considering working under Python3.7, but other options are possible).

1 - AWS Quick  Start Templates

8 - Python 3.7

Project name [sam-app]: some_module_layer

1 - Hello World Example

2. Modify requirements.txt file

cd some_module_layer
vim hello_world/requirements.txt

this will open requirements.txt file on vim, on Windows you could type instead code hello_world/requirements.txt to edit the file on Visual Studio Code.

3. Add pyarrow to requirements.txt

Alongside pyarrow, it will work to include additionnaly pandas and s3fs. In this case including pandas will avoid it to not recognize pyarrow as an engine to read parquet files.

pandas
pyarrow
s3fs

4. Build with a container

Docker is required to use the option --use-container when running the sam build command. If it's the first time, it will pull the lambci/lambda:build-python3.7 Docker  image.

sam build --use-container
rm .aws-sam/build/HelloWorldFunction/app.py
rm .aws-sam/build/HelloWorldFunction/__init__.py
rm .aws-sam/build/HelloWorldFunction/requirements.txt

notice that we're keeping only the python libraries.

5. Zip files

cp -r .aws-sam/build/HelloWorldFunction/ python/
zip -r some_module_layer.zip python/

On Windows, it would work to run  Compress-Archive python/ some_module_layer.zip.

6. Upload zip file to AWS

The following link is useful for this.


问:
NGXS returning only needed values
Hi I have this NGXS script

```<code>@Action(Login)
  login(ctx: StateContext<AuthStateModel>, action: Login) {
    return this.authService.login(action.payload).pipe(
      tap((result: { token: string }) => {
        ctx.patchState({
          token: result.token,
          username: action.payload.username
        });
      })
    );
  }
</code>```

However on my dispatch subscribe, I get the entire store returned. I only want what is needed. Is there a way to just get the token and username with a subscription?

Best regards.

答:
If you want to see the updated username/token values then the simplest way is to use withLatestFrom in combination with your dispatch in the component.

E.g. in the component you'd have a selector

@Select(AuthState) authState$: Observable<AuthStateModel>;

When you dispatch the action ..

```<code>this.store.dispatch(new Login(..))
.pipe(
   withLatestFrom(this.authState$)
  )
  .subscribe(([_, authData]) => {
    // do something with authData.token / username
  });
</code>```

This approach is shown here in the docs for dispatching actions.

Title: NGXS returning only needed values

Body:
Hi I have this NGXS script

@Action(Login)
  login(ctx: StateContext<AuthStateModel>, action: Login) {
    return this.authService.login(action.payload).pipe(
      tap((result: { token: string }) => {
        ctx.patchState({
          token: result.token,
          username: action.payload.username
        });
      })
    );
  }

However on my dispatch subscribe, I get the entire store returned. I only want what is needed. Is there a way to just get the token and username with a subscription?

Best regards.

Body:
If you want to see the updated username/token values then the simplest way is to use withLatestFrom in combination with your dispatch in the component.

E.g. in the component you'd have a selector

@Select(AuthState) authState$: Observable<AuthStateModel>;

When you dispatch the action ..

this.store.dispatch(new Login(..))
.pipe(
   withLatestFrom(this.authState$)
  )
  .subscribe(([_, authData]) => {
    // do something with authData.token / username
  });

This approach is shown here in the docs for dispatching actions.


问:
cs50 pset7 Movies sql.12
Could someone help me with how to approach fixing my SQL?
I am handling five tables.

The expected results are,

"write a SQL query to list the titles of all movies in which both
Johnny Depp and Helena Bonham Carter starred"

Your query should output a table with a single column for the title of each movie.

You may assume that there is only one person in the database with
the name Johnny Depp.

You may assume that there is only one person in the database with the name Helena Bonham Carter.

```<code> CREATE TABLE movies (
                     id INTEGER,
                     title TEXT NOT NULL,
                     year NUMERIC,
                     PRIMARY KEY(id)
                    );

 CREATE TABLE stars (
                 movie_id INTEGER NOT NULL,
                 person_id INTEGER NOT NULL,
                 FOREIGN KEY(movie_id) REFERENCES movies(id),
                 FOREIGN KEY(person_id) REFERENCES people(id)
                   );

CREATE TABLE directors (
                 movie_id INTEGER NOT NULL,
                 person_id INTEGER NOT NULL,
                 FOREIGN KEY(movie_id) REFERENCES movies(id),
                 FOREIGN KEY(person_id) REFERENCES people(id)
             );

CREATE TABLE ratings (
                 movie_id INTEGER NOT NULL,
                 rating REAL NOT NULL,
                 votes INTEGER NOT NULL,
                 FOREIGN KEY(movie_id) REFERENCES movies(id)
               );

CREATE TABLE people (
                 id INTEGER,
                 name TEXT NOT NULL,
                 birth NUMERIC,
                 PRIMARY KEY(id)
             );
</code>```

This results in 59 rows, where it is supposed to be only 6 rows.

```<code> SELECT title FROM movies  WHERE id IN (SELECT DISTINCT movie_id FROM
 stars WHERE person_id =  (SELECT id FROM people  WHERE name IN
 ("Johnny Depp", "Helena Bonham Carter")));
</code>```

I see other posts talking about using "WHERE IN" would be helpful, and I am indeed using it.

答:
Using JOIN and INTERSECT helped me solve this one.

Something like this:

```<code>Select title from movies
join stars on stars.movie_id = movies.id
join people on people.id = stars.person_id
where people.name = "Helena Bonham Carter"
INTERSECT
Select title from movies
join stars on stars.movie_id = movies.id
join people on people.id = stars.person_id
where people.name = "Johnny Depp";
</code>```

Title: cs50 pset7 Movies sql.12

Body:
Could someone help me with how to approach fixing my SQL?
I am handling five tables.

The expected results are,

"write a SQL query to list the titles of all movies in which both
Johnny Depp and Helena Bonham Carter starred"

Your query should output a table with a single column for the title of each movie.

You may assume that there is only one person in the database with
the name Johnny Depp.

You may assume that there is only one person in the database with the name Helena Bonham Carter.

 CREATE TABLE movies (
                     id INTEGER,
                     title TEXT NOT NULL,
                     year NUMERIC,
                     PRIMARY KEY(id)
                    );

 CREATE TABLE stars (
                 movie_id INTEGER NOT NULL,
                 person_id INTEGER NOT NULL,
                 FOREIGN KEY(movie_id) REFERENCES movies(id),
                 FOREIGN KEY(person_id) REFERENCES people(id)
                   );

CREATE TABLE directors (
                 movie_id INTEGER NOT NULL,
                 person_id INTEGER NOT NULL,
                 FOREIGN KEY(movie_id) REFERENCES movies(id),
                 FOREIGN KEY(person_id) REFERENCES people(id)
             );

CREATE TABLE ratings (
                 movie_id INTEGER NOT NULL,
                 rating REAL NOT NULL,
                 votes INTEGER NOT NULL,
                 FOREIGN KEY(movie_id) REFERENCES movies(id)
               );

CREATE TABLE people (
                 id INTEGER,
                 name TEXT NOT NULL,
                 birth NUMERIC,
                 PRIMARY KEY(id)
             );

This results in 59 rows, where it is supposed to be only 6 rows.

 SELECT title FROM movies  WHERE id IN (SELECT DISTINCT movie_id FROM
 stars WHERE person_id =  (SELECT id FROM people  WHERE name IN
 ("Johnny Depp", "Helena Bonham Carter")));

I see other posts talking about using "WHERE IN" would be helpful, and I am indeed using it.

Body:
Using JOIN and INTERSECT helped me solve this one.

Something like this:

Select title from movies
join stars on stars.movie_id = movies.id
join people on people.id = stars.person_id
where people.name = "Helena Bonham Carter"
INTERSECT
Select title from movies
join stars on stars.movie_id = movies.id
join people on people.id = stars.person_id
where people.name = "Johnny Depp";


问:
Hide navigation bar on scroll in SwiftUI?
Hiding the navigation bar on scroll was supported in Swift with navigationController?.hidesBarsOnSwipe = true

To be clear, I'd like it to only be hidden on scroll, so .navigationBarHidden(true) would not suffice.

I tried accessing the NavigationController as described in this Stackoverflow answer, (I added nc.hidesBarsOnSwipe = true) and while it compiled, it did not work.

Is this supported in SwiftUI?

答:
NavigationView seems to be relatively buggy still.  For example, by default a ScrollView will ignore the title area and just scroll beneath it.

It looks to me like you can get this working by using displayMode: .inline and StackNavigationViewStyle() together.

struct ContentView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                ForEach(0...20, id: \.self) { count in
                    (count % 2 == 0 ? Color.red : Color.blue)
                        .frame(height: 44.0)
                }
            }
            .background(NavigationConfigurator { nc in // NavigationConfigurator is from the OP's post: https://stackoverflow.com/a/58427754/7834914
                nc.hidesBarsOnSwipe = true
            })
            .navigationBarTitle("Hello World", displayMode: .inline)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}
```

Before Scroll:

After Scroll:

Body:
NavigationView seems to be relatively buggy still.  For example, by default a ScrollView will ignore the title area and just scroll beneath it.

It looks to me like you can get this working by using displayMode: .inline and StackNavigationViewStyle() together.

struct ContentView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                ForEach(0...20, id: \.self) { count in
                    (count % 2 == 0 ? Color.red : Color.blue)
                        .frame(height: 44.0)
                }
            }
            .background(NavigationConfigurator { nc in // NavigationConfigurator is from the OP's post: https://stackoverflow.com/a/58427754/7834914
                nc.hidesBarsOnSwipe = true
            })
            .navigationBarTitle("Hello World", displayMode: .inline)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

Before Scroll:

After Scroll:


问:
Axios put error 500 saying field cannot be empty
So i am trying to update a user from the admin panel, i get the values from the form through a v-model and then i do my axios.put with all the data in a FormData(). The issue is that i get an error 500 saying that x field can't be empty, so i guess my FormData() isn't being passed on to the controller.
I tried with a post method and it worked fine. So i wonder what i did wrong ?
Here's the Vuejs code :

```<code><script>
export default {
    data () {
      return {
        search: '',
        headers: [
          { text: 'ID', align: 'start', value: 'id'},
          { text: 'Name', value: 'name' },
          { text: 'Profile', value: 'profile' },
          { text: 'Email', value: 'email' },
          { text: 'Created At', value: 'created_at' },
          { text: 'Actions', value: 'actions' },
        ],
        users: null,
        editedUser: null,
        dialog: false,
      }
    },
    methods: {
        getUsers(){
            axios.get('../admin/getUsers')
        .then(response => this.users = response.data)
        },
        editUser(item){
          this.editedUser = item;
          this.dialog = true;
        },
        submitEditedUser(){
        let data = new FormData();

        data.append('id', this.editedUser.id)
        data.append('name', this.editedUser.name)
        data.append('profile', this.editedUser.profile)
        data.append('email', this.editedUser.email)

          axios.put('../admin/updateUser/' + this.editedUser.id, data)
          .then(res => {
          console.log('done');
          })
          .catch(err => {
          console.log('changes failed');
          })
        }
    },
    mounted(){
        this.getUsers();
    }

  }
</script>
</code>```

And here's the controller code :

```<code>    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function updateUser($id, Request $request)
    {
        $user = User::find($id);

        $user->name = request('name');
        $user->profile = request('profile');
        $user->email = request("email");

        $user->save();
    }

</code>```

Everything works fine with post so nothing's wrong with the variable or the form, i think it really is just the data that's not being passed on. Although i want to use the put method because it is originally used for updating data.
Thanks !

答:
can you try this? if this not work you can dd(request) inside the controller and see if is empty</p>

```<code>public function updateUser($id, Request $request)
{

    $user = User::find($id);

    $user->name = $request->data['name'];
    $user->profile = $request->data['profile'];
    $user->email = $request->data['email'];

    $user->save();
}
</code>```

Body:
So i am trying to update a user from the admin panel, i get the values from the form through a v-model and then i do my axios.put with all the data in a FormData(). The issue is that i get an error 500 saying that x field can't be empty, so i guess my FormData() isn't being passed on to the controller.
I tried with a post method and it worked fine. So i wonder what i did wrong ?
Here's the Vuejs code :

And here's the controller code :

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function updateUser($id, Request $request)
    {
        $user = User::find($id);

        $user->name = request('name');
        $user->profile = request('profile');
        $user->email = request("email");

        $user->save();
    }

Everything works fine with post so nothing's wrong with the variable or the form, i think it really is just the data that's not being passed on. Although i want to use the put method because it is originally used for updating data.
Thanks !

Body:
can you try this? if this not work you can dd(request) inside the controller and see if is empty

public function updateUser($id, Request $request)
{

    $user = User::find($id);

    $user->name = $request->data['name'];
    $user->profile = $request->data['profile'];
    $user->email = $request->data['email'];

    $user->save();
}


问:
rtmidi's get_ports() returns invalid result with Flask views
I am trying to use python-rtmidi with Flask and trying to display all available MIDI ports on the browser. And noticing two issues while the function gets called from views.py.

MidiIn().get_ports() : doesn't recognize a new ports that opened after the flask server started.

If a port reopened after it was closed (ex: VMPK Output) calling the same function MidiIn().get_ports(), it wouldn't recognize the new port.

Calling MidiIn().get_ports() from any module works fine. It just whenever it gets called from views.py, the above issues occured.

A similar code works totally fine in Quart framework.

Any idea why it happens with Flask and would appreciate if you could shed some light on a workaround.

import time

from flask import Flask, jsonify
from rtmidi import MidiIn

app = Flask(__name__)

def works_fine():
    print(MidiIn().get_ports())  # ==> Outputs: ['Network Session 1', 'IAC Driver Bus 1']

    time.sleep(5)  # <== Open a MIDI port during this time
    print(MidiIn().get_ports())  # ==> Outputs: ['Network Session 1', 'IAC Driver Bus 1', 'VMPK Output']]

    time.sleep(5)  # <== Close a MIDI port during this time
    print(MidiIn().get_ports())  # ==> Outputs: ['Network Session 1', 'IAC Driver Bus 1']

@app.route('/test')
def wont_work():
    # Restart the server
    # and make sure a MIDI device is connected
    # visit : http://127.0.0.1:5000//test
    print(MidiIn().get_ports())  # ==> Outputs: ['Network Session 1', 'IAC Driver Bus 1', 'VMPK Output']]

    time.sleep(5)  # <== Close a MIDI port during this time
    print(MidiIn().get_ports())  # ==> Outputs: ['Network Session 1', 'IAC Driver Bus 1']
    # Tbe above code also returns ['Network Session 1', 'IAC Driver Bus 1', None] in other tests

    time.sleep(5)  # <== Open a MIDI port during this time
    print(MidiIn().get_ports())  # ==> Outputs: ['Network Session 1', 'IAC Driver Bus 1']

    return jsonify({
        'output': MidiIn().get_ports()
    })
```

答:
Answer my own question.  Not sure what's difference but running the code with flask run causing this issue but python -m app works fine.  So I just have to call the werkzeug myself (with Flask defaults except a couple).

Below results are for after a MIDI port closed:

flask run

Output:

```<code>{
"ports":["Network Session 1","IAC Driver Bus 1",null]
}
</code>```

python -m app

Output:

```<code>{
"ports":["Network Session 1","IAC Driver Bus 1"]
}
</code>```

from flask import Flask, jsonify

def create_app():
    app = Flask(__name__)

    # Blueprints WORKS as well
    # from views import bp
    # app.register_blueprint(bp)

    def get_inports():
        from rtmidi import MidiIn

        inports = MidiIn().get_ports()
        return inports

    @app.route('/')
    def hello_world():
        return jsonify({
            'ports': get_inports()
        })

    return app

if __name__ == '__main__':
    from werkzeug.serving import run_simple

    run_simple(
        hostname='127.0.0.1',
        port=5000,
        application=create_app(),
        use_reloader=True,
        use_debugger=True,
    )

```

Body:
Answer my own question.  Not sure what's difference but running the code with flask run causing this issue but python -m app works fine.  So I just have to call the werkzeug myself (with Flask defaults except a couple).

Below results are for after a MIDI port closed:

flask run

Output:

{
"ports":["Network Session 1","IAC Driver Bus 1",null]
}

python -m app

Output:

{
"ports":["Network Session 1","IAC Driver Bus 1"]
}

from flask import Flask, jsonify

def create_app():
    app = Flask(__name__)

    # Blueprints WORKS as well
    # from views import bp
    # app.register_blueprint(bp)

    def get_inports():
        from rtmidi import MidiIn

        inports = MidiIn().get_ports()
        return inports

    @app.route('/')
    def hello_world():
        return jsonify({
            'ports': get_inports()
        })

    return app

if __name__ == '__main__':
    from werkzeug.serving import run_simple

    run_simple(
        hostname='127.0.0.1',
        port=5000,
        application=create_app(),
        use_reloader=True,
        use_debugger=True,
    )


问:
Var type string and not int
Why is the var type still str in Case 1?. Shouldn't it be int?
In Case 2 when I'm not using the function it changes to int.

```<code>#CASE 1
num1=str()

def suma():

    num1=int(7)

suma()

print(type(num1)) # <class 'str'>

#CASE 2
num1=str()
num1=7

print(type(num1)) # <class 'int'>
</code>```

答:
num1 is showing string because it is global variable.

If you want to change the type of num1 from function suma(), then use global keyword as global num1.

```<code># your code goes here
#CASE 1
num1=str()

def suma():
    global num1
    num1=int(7)

    print(type(num1)) #<class 'int'>
suma()

print(type(num1)) # <class 'int'>

#CASE 2
num1=str()
num1=7

print(type(num1)) # <class 'int'>
</code>```

Title: Var type string and not int

Body:
Why is the var type still str in Case 1?. Shouldn't it be int?
In Case 2 when I'm not using the function it changes to int.

#CASE 1
num1=str()

def suma():

    num1=int(7)

suma()

print(type(num1)) # <class 'str'>

#CASE 2
num1=str()
num1=7

print(type(num1)) # <class 'int'>

Body:
num1 is showing string because it is global variable.

If you want to change the type of num1 from function suma(), then use global keyword as global num1.

# your code goes here
#CASE 1
num1=str()

def suma():
    global num1
    num1=int(7)

    print(type(num1)) #<class 'int'>
suma()

print(type(num1)) # <class 'int'>

#CASE 2
num1=str()
num1=7

print(type(num1)) # <class 'int'>


问:
What is the Best Way to Clean a URL with a Title in it
What is the best way to clean a URL? I am looking for a URL like this

what_is_the_best_headache_medication

My current code

```<code>public string CleanURL(string str)
{
    str = str.Replace("!", "");
    str = str.Replace("@", "");
    str = str.Replace("#", "");
    str = str.Replace("$", "");
    str = str.Replace("%", "");
    str = str.Replace("^", "");
    str = str.Replace("&", "");
    str = str.Replace("*", "");
    str = str.Replace("(", "");
    str = str.Replace(")", "");
    str = str.Replace("-", "");
    str = str.Replace("_", "");
    str = str.Replace("+", "");
    str = str.Replace("=", "");
    str = str.Replace("{", "");
    str = str.Replace("[", "");
    str = str.Replace("]", "");
    str = str.Replace("}", "");
    str = str.Replace("|", "");
    str = str.Replace(@"\", "");
    str = str.Replace(":", "");
    str = str.Replace(";", "");
    str = str.Replace(@"\", "");
    str = str.Replace("'", "");
    str = str.Replace("<", "");
    str = str.Replace(">", "");
    str = str.Replace(",", "");
    str = str.Replace(".", "");
    str = str.Replace("`", "");
    str = str.Replace("~", "");
    str = str.Replace("/", "");
    str = str.Replace("?", "");
    str = str.Replace("  ", " ");
    str = str.Replace("   ", " ");
    str = str.Replace("    ", " ");
    str = str.Replace("     ", " ");
    str = str.Replace("      ", " ");
    str = str.Replace("       ", " ");
    str = str.Replace("        ", " ");
    str = str.Replace("         ", " ");
    str = str.Replace("          ", " ");
    str = str.Replace("           ", " ");
    str = str.Replace("            ", " ");
    str = str.Replace("             ", " ");
    str = str.Replace("              ", " ");
    str = str.Replace(" ", "_");
    return str;
}
</code>```

答:
Generally your best bet is to go with a white list regular expression approach instead of removing all the unwanted characters because you definitely are going to miss some.

The answers here are fine so far but I personally did not want to remove umlauts and characters with accent marks entirely. So the final solution I came up with looks like this:

```<code>public static string CleanUrl(string value)
{
    if (value.IsNullOrEmpty())
        return value;

    // replace hyphens to spaces, remove all leading and trailing whitespace
    value = value.Replace("-", " ").Trim().ToLower();

    // replace multiple whitespace to one hyphen
    value = Regex.Replace(value, @"[\s]+", "-");

    // replace umlauts and eszett with their equivalent
    value = value.Replace("ß", "ss");
    value = value.Replace("ä", "ae");
    value = value.Replace("ö", "oe");
    value = value.Replace("ü", "ue");

    // removes diacritic marks (often called accent marks) from characters
    value = RemoveDiacritics(value);

    // remove all left unwanted chars (white list)
    value = Regex.Replace(value, @"[^a-z0-9\s-]", String.Empty);

    return value;
}
</code>```

The used RemoveDiacritics method is based on the SO answer by Blair Conrad:

```<code>public static string RemoveDiacritics(string value)
{
    if (value.IsNullOrEmpty())
        return value;

    string normalized = value.Normalize(NormalizationForm.FormD);
    StringBuilder sb = new StringBuilder();

    foreach (char c in normalized)
    {
        if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
            sb.Append(c);
    }

    Encoding nonunicode = Encoding.GetEncoding(850);
    Encoding unicode = Encoding.Unicode;

    byte[] nonunicodeBytes = Encoding.Convert(unicode, nonunicode, unicode.GetBytes(sb.ToString()));
    char[] nonunicodeChars = new char[nonunicode.GetCharCount(nonunicodeBytes, 0, nonunicodeBytes.Length)];
    nonunicode.GetChars(nonunicodeBytes, 0, nonunicodeBytes.Length, nonunicodeChars, 0);

    return new string(nonunicodeChars);
}
</code>```

Hope that helps somebody challenged by slugifying URLs and keeping umlauts and friends with their URL friendly equivalent at the same time.

ContentLicense: CC BY-SA 2.5

Body:
What is the best way to clean a URL? I am looking for a URL like this

what_is_the_best_headache_medication

My current code

public string CleanURL(string str)
{
    str = str.Replace("!", "");
    str = str.Replace("@", "");
    str = str.Replace("#", "");
    str = str.Replace("$", "");
    str = str.Replace("%", "");
    str = str.Replace("^", "");
    str = str.Replace("&", "");
    str = str.Replace("*", "");
    str = str.Replace("(", "");
    str = str.Replace(")", "");
    str = str.Replace("-", "");
    str = str.Replace("_", "");
    str = str.Replace("+", "");
    str = str.Replace("=", "");
    str = str.Replace("{", "");
    str = str.Replace("[", "");
    str = str.Replace("]", "");
    str = str.Replace("}", "");
    str = str.Replace("|", "");
    str = str.Replace(@"\", "");
    str = str.Replace(":", "");
    str = str.Replace(";", "");
    str = str.Replace(@"\", "");
    str = str.Replace("'", "");
    str = str.Replace("<", "");
    str = str.Replace(">", "");
    str = str.Replace(",", "");
    str = str.Replace(".", "");
    str = str.Replace("`", "");
    str = str.Replace("~", "");
    str = str.Replace("/", "");
    str = str.Replace("?", "");
    str = str.Replace("  ", " ");
    str = str.Replace("   ", " ");
    str = str.Replace("    ", " ");
    str = str.Replace("     ", " ");
    str = str.Replace("      ", " ");
    str = str.Replace("       ", " ");
    str = str.Replace("        ", " ");
    str = str.Replace("         ", " ");
    str = str.Replace("          ", " ");
    str = str.Replace("           ", " ");
    str = str.Replace("            ", " ");
    str = str.Replace("             ", " ");
    str = str.Replace("              ", " ");
    str = str.Replace(" ", "_");
    return str;
}

Body:
Generally your best bet is to go with a white list regular expression approach instead of removing all the unwanted characters because you definitely are going to miss some.

The answers here are fine so far but I personally did not want to remove umlauts and characters with accent marks entirely. So the final solution I came up with looks like this:

public static string CleanUrl(string value)
{
    if (value.IsNullOrEmpty())
        return value;

    // replace hyphens to spaces, remove all leading and trailing whitespace
    value = value.Replace("-", " ").Trim().ToLower();

    // replace multiple whitespace to one hyphen
    value = Regex.Replace(value, @"[\s]+", "-");

    // replace umlauts and eszett with their equivalent
    value = value.Replace("ß", "ss");
    value = value.Replace("ä", "ae");
    value = value.Replace("ö", "oe");
    value = value.Replace("ü", "ue");

    // removes diacritic marks (often called accent marks) from characters
    value = RemoveDiacritics(value);

    // remove all left unwanted chars (white list)
    value = Regex.Replace(value, @"[^a-z0-9\s-]", String.Empty);

    return value;
}

The used RemoveDiacritics method is based on the SO answer by Blair Conrad:

public static string RemoveDiacritics(string value)
{
    if (value.IsNullOrEmpty())
        return value;

    string normalized = value.Normalize(NormalizationForm.FormD);
    StringBuilder sb = new StringBuilder();

    foreach (char c in normalized)
    {
        if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
            sb.Append(c);
    }

    Encoding nonunicode = Encoding.GetEncoding(850);
    Encoding unicode = Encoding.Unicode;

    byte[] nonunicodeBytes = Encoding.Convert(unicode, nonunicode, unicode.GetBytes(sb.ToString()));
    char[] nonunicodeChars = new char[nonunicode.GetCharCount(nonunicodeBytes, 0, nonunicodeBytes.Length)];
    nonunicode.GetChars(nonunicodeBytes, 0, nonunicodeBytes.Length, nonunicodeChars, 0);

    return new string(nonunicodeChars);
}

Hope that helps somebody challenged by slugifying URLs and keeping umlauts and friends with their URL friendly equivalent at the same time.


问:
How to find a whole word in a sentence in Python?
Say I have a sentence

```<code>sent = "Luxred is a great,awesome and amazing but, they make a lot of money too"
ents = ['L'Oreal', 'Maybelline', 'Garnier Natural', 'Luxred', 'Lux']
</code>```

The sentences will be like this. The sentences will have commas and other words too. I want to find the exact word match for Luxred. It shouldn't match for Lux either and also want the starting index & ending index of the word. Here it is (0, 5). The sentence might change and the search words might come in between etc. How do I do this ?

Already tried using find.

```<code>def annotFinal (label, entities, sentences) :
finaldict = []
for sent in sentences :
    sent = str(sent).lower()
    inner_list = []
    for ent in entities :
        if (sent.find(ent) != -1) :
            inner_tup = (sent.find(ent), sent.find(ent) + len(ent), label)
            inner_list.append(inner_tup)
    inner_dict={'entities': sorted(inner_list, key=lambda x: x[0])}
    finaldict.append((sent, inner_dict))
return finaldict
</code>```

答:
```<code>import re
re.search('r\bLuxred\b',sent)
</code>```

If you need need everything in ents then you could do:

```<code>d = {i:re.search(f'\\b{i}\\b',sent) for i in ents}
d['Luxred']
</code>```

Body:
Say I have a sentence

sent = "Luxred is a great,awesome and amazing but, they make a lot of money too"
ents = ['L'Oreal', 'Maybelline', 'Garnier Natural', 'Luxred', 'Lux']

The sentences will be like this. The sentences will have commas and other words too. I want to find the exact word match for Luxred. It shouldn't match for Lux either and also want the starting index & ending index of the word. Here it is (0, 5). The sentence might change and the search words might come in between etc. How do I do this ?

Already tried using find.

def annotFinal (label, entities, sentences) :
finaldict = []
for sent in sentences :
    sent = str(sent).lower()
    inner_list = []
    for ent in entities :
        if (sent.find(ent) != -1) :
            inner_tup = (sent.find(ent), sent.find(ent) + len(ent), label)
            inner_list.append(inner_tup)
    inner_dict={'entities': sorted(inner_list, key=lambda x: x[0])}
    finaldict.append((sent, inner_dict))
return finaldict

Body:
import re
re.search('r\bLuxred\b',sent)

If you need need everything in ents then you could do:

d = {i:re.search(f'\\b{i}\\b',sent) for i in ents}
d['Luxred']


问:
Run java code passing file as input argument on Jetbrain's Intelij IDEA
I'm trying to run a Java code using on Intelij IDEA passing a .txt file as argument.

I followed this up this discussion but it didn't help much Run Program from IntelliJ with Command Line File Input

Also, I trid the EXACT same thing (I guess..) in another computer and it worked, but in my personal computer I have a newer version of Intelij and some little things are different. I don't know if something has changed or if I'm doing something wrong...

Here is my code:

```<code> public static void main(String[] args) {
        // read the n points from a file
        StdOut.println("Args: " + Arrays.toString(args));
        StdOut.println("Args: " + args[0]);
    }
</code>```

My configurations:

run configuration

And the output:

```<code>Args: []
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at BruteCollinearPoints.main(BruteCollinearPoints.java:89)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:128)

Process finished with exit code 1
</code>```

If I run this code at the console everything goes fine.

答: Try giving the name of the file on program arguments. Put one space between each argument. Since you didn't give any argument, you are facing with this problem. Also the files must be in the same folder with your project folder.</p>

Body:
I'm trying to run a Java code using on Intelij IDEA passing a .txt file as argument.

I followed this up this discussion but it didn't help much Run Program from IntelliJ with Command Line File Input

Also, I trid the EXACT same thing (I guess..) in another computer and it worked, but in my personal computer I have a newer version of Intelij and some little things are different. I don't know if something has changed or if I'm doing something wrong...

Here is my code:

 public static void main(String[] args) {
        // read the n points from a file
        StdOut.println("Args: " + Arrays.toString(args));
        StdOut.println("Args: " + args[0]);
    }

My configurations:

run configuration

And the output:

Args: []
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at BruteCollinearPoints.main(BruteCollinearPoints.java:89)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:128)

Process finished with exit code 1

If I run this code at the console everything goes fine.


问:
Where to handle IOException when adding a file handler to a logger in a class?
I am trying to set up a logging system for my application.

I have seen here that it is possible to send logs to an individual file. The code is as follows:

package com.javacodegeeks.snippets.core;

import java.util.logging.FileHandler;
import java.util.logging.Logger;

public class WriteLogEntriesToLogFile {

    public static void main(String[] args) throws Exception {

        boolean append = true;
        FileHandler handler = new FileHandler("default.log", append);

        Logger logger = Logger.getLogger("com.javacodegeeks.snippets.core");
        logger.addHandler(handler);

        logger.severe("severe message");

        logger.warning("warning message");

        logger.info("info message");

        logger.config("config message");

        logger.fine("fine message");

        logger.finer("finer message");

        logger.finest("finest message");

    }

}
```

<p>In my case, I would like to add this logging to a Class. I attach a basic class and what I have tried (the class is a singleton, so that fact is included in the example):</p>

<pre class="lang-java prettyprint-override"><code>public class MyClass {

    FileHandler handler = new FileHandler("MyClass.log", false);
    Logger logger = Logger.getLogger(MyClass.class.getName());

    // SINGLETON PATTERN
    private static final MyClass SINGLE_INSTANCE = new MyClass();
    private MyClass() {
    }
    public static MyClass getInstance() {
      return SINGLE_INSTANCE;
    }

    public void doStuff() {

        Logger.getLogger(MyClass.class.getName()).log(Level.FINE, "Log Text");

    }

}
</code>```

But I get:

In FileHandler handler = new FileHandler("OrderFlowDrawer.log", false);: unreported IOException.

In Logger logger = Logger.getLogger(MyClass.class.getName());: the logger declaration shall be static and final.

From that I have the following questions/doubts I can not fully understand:

How shall I handle the IOException?

Why the logger declaration shall be static and final?

I am using Java 8 in case it could be relevant.

答:
How shall I handle the IOException?

This is a good question and there are many different answers.  I often still use System.err.println(...) to report just these sorts of issues.  Specifically initialization issues where the logging subsystem, configuration, or other startup operations have failed.  This means that when I run my application, there is still some sort of console.log.  This also works to capture the rare JVM fault or message emitted by poorly behaving 3rd party software to stderr.

This means that if the application fails fast because of some sort of bootstrap issue, I go looking in the console.log to see if there is any information there.  This also means that I should emit "System is running" type of message to the console.log so I know when to switch to looking at my logging files instead of the console if there are problems.

If the Logger constructor throws IOException, then for static fields, I catch and rethrow the exception as a RuntimeException.  Something like:

```<code>private static final Logger;

// this static black is executed before the class constructor and is the way
// that I initialize static fields that throw exceptions
static {
   try {
      logger = Logger.getLogger(MyClass.class.getName());
   } catch (IOExcepion ioe) {
      System.err.println("Initialization of logger threw");
      ioe.printStackTrace();
      // this will cause the class to not initialize and typically brings down the JVM
      throw new RuntimeException("Initialization of logger threw", ioe);
   }
}
</code>```

Why the logger declaration shall be static and final?

static fields are shared between multiple instances of a class.  Typically if you have multiple MyClass instances, they should share the same instance of the logger and not have one per class instance.  That said, once and a while I do want a per-class-instance logger because I want to separate the logging prefix, but it is rare.

final may be important, depending on the logger implementation in terms of ensuring that the logging class is fully initialized before it is used in terms of thread memory sharing.  final fields must be fully constructed and the compiler is not able to delay their initialization.  Given that the logger may be used by multiple threads, this is important.  final also ensures that some misbehaving part of the code doesn't change the value of the logger or set it to null.  Different parts of your classes will be relying on the logger field having a value and don't want to have to do if (logger != null) ... type patterns everywhere.

Body:
I am trying to set up a logging system for my application.

I have seen here that it is possible to send logs to an individual file. The code is as follows:

package com.javacodegeeks.snippets.core;

import java.util.logging.FileHandler;
import java.util.logging.Logger;

public class WriteLogEntriesToLogFile {

    public static void main(String[] args) throws Exception {

        boolean append = true;
        FileHandler handler = new FileHandler("default.log", append);

        Logger logger = Logger.getLogger("com.javacodegeeks.snippets.core");
        logger.addHandler(handler);

        logger.severe("severe message");

        logger.warning("warning message");

        logger.info("info message");

        logger.config("config message");

        logger.fine("fine message");

        logger.finer("finer message");

        logger.finest("finest message");

    }

}

In my case, I would like to add this logging to a Class. I attach a basic class and what I have tried (the class is a singleton, so that fact is included in the example):

public class MyClass {

    FileHandler handler = new FileHandler("MyClass.log", false);
    Logger logger = Logger.getLogger(MyClass.class.getName());

    // SINGLETON PATTERN
    private static final MyClass SINGLE_INSTANCE = new MyClass();
    private MyClass() {
    }
    public static MyClass getInstance() {
      return SINGLE_INSTANCE;
    }

    public void doStuff() {

        Logger.getLogger(MyClass.class.getName()).log(Level.FINE, "Log Text");

    }

}

But I get:

In FileHandler handler = new FileHandler("OrderFlowDrawer.log", false);: unreported IOException.

In Logger logger = Logger.getLogger(MyClass.class.getName());: the logger declaration shall be static and final.

From that I have the following questions/doubts I can not fully understand:

How shall I handle the IOException?

Why the logger declaration shall be static and final?

I am using Java 8 in case it could be relevant.

Body:
How shall I handle the IOException?

This is a good question and there are many different answers.  I often still use System.err.println(...) to report just these sorts of issues.  Specifically initialization issues where the logging subsystem, configuration, or other startup operations have failed.  This means that when I run my application, there is still some sort of console.log.  This also works to capture the rare JVM fault or message emitted by poorly behaving 3rd party software to stderr.

This means that if the application fails fast because of some sort of bootstrap issue, I go looking in the console.log to see if there is any information there.  This also means that I should emit "System is running" type of message to the console.log so I know when to switch to looking at my logging files instead of the console if there are problems.

If the Logger constructor throws IOException, then for static fields, I catch and rethrow the exception as a RuntimeException.  Something like:

private static final Logger;

// this static black is executed before the class constructor and is the way
// that I initialize static fields that throw exceptions
static {
   try {
      logger = Logger.getLogger(MyClass.class.getName());
   } catch (IOExcepion ioe) {
      System.err.println("Initialization of logger threw");
      ioe.printStackTrace();
      // this will cause the class to not initialize and typically brings down the JVM
      throw new RuntimeException("Initialization of logger threw", ioe);
   }
}

Why the logger declaration shall be static and final?

static fields are shared between multiple instances of a class.  Typically if you have multiple MyClass instances, they should share the same instance of the logger and not have one per class instance.  That said, once and a while I do want a per-class-instance logger because I want to separate the logging prefix, but it is rare.

final may be important, depending on the logger implementation in terms of ensuring that the logging class is fully initialized before it is used in terms of thread memory sharing.  final fields must be fully constructed and the compiler is not able to delay their initialization.  Given that the logger may be used by multiple threads, this is important.  final also ensures that some misbehaving part of the code doesn't change the value of the logger or set it to null.  Different parts of your classes will be relying on the logger field having a value and don't want to have to do if (logger != null) ... type patterns everywhere.


问:
How do I change the background color in JavaScript?
I am trying to change the background color of the wrapper every time I click the button. When it is clicked, it is supposed to change to a random color that is in the array. Any thoughts for a beginner? Also the CSS was done with SASS so it may not look pretty.

function change_Color() {
  var color = ["blue", "red", "green", "yellow"];
  document.getElementById("wrapper").style.backgroundColor = color;
}```
<pre class="snippet-code-css lang-css prettyprint-override"><code>body {
  margin: 0;
}

* {
  box-sizing: border-box;
}

#wrapper {
  width: 100%;
  height: 100vh;
  background-color: skyblue;
  display: flex;
  justify-content: center;
  align-items: center;
  button {
    padding: 10px;
    background: orange;
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 2rem;
    transition: .5s ease;
    &:hover {
      box-shadow: 0 4px 3px 0px rgba(255, 255, 255, 0.8);
      cursor: pointer;
    }
    &:focus {
      outline: 0;
      color: skyblue;
    }
  }
}</code>```

  <button onclick="change_Color()">Click me!</button>

```

答:
To pick a random color just use the Math.random() function:

```<code>var colors = ["blue", "red", "green", "yellow"];
var color = colors[Math.floor(Math.random() * colors.length)]
document.getElementById("wrapper").style.backgroundColor = color;
</code>```

Body:
I am trying to change the background color of the wrapper every time I click the button. When it is clicked, it is supposed to change to a random color that is in the array. Any thoughts for a beginner? Also the CSS was done with SASS so it may not look pretty.

function change_Color() {
  var color = ["blue", "red", "green", "yellow"];
  document.getElementById("wrapper").style.backgroundColor = color;
}

body {
  margin: 0;
}

* {
  box-sizing: border-box;
}

#wrapper {
  width: 100%;
  height: 100vh;
  background-color: skyblue;
  display: flex;
  justify-content: center;
  align-items: center;
  button {
    padding: 10px;
    background: orange;
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 2rem;
    transition: .5s ease;
    &:hover {
      box-shadow: 0 4px 3px 0px rgba(255, 255, 255, 0.8);
      cursor: pointer;
    }
    &:focus {
      outline: 0;
      color: skyblue;
    }
  }
}

  <button onclick="change_Color()">Click me!</button>

Body:
To pick a random color just use the Math.random() function:

var colors = ["blue", "red", "green", "yellow"];
var color = colors[Math.floor(Math.random() * colors.length)]
document.getElementById("wrapper").style.backgroundColor = color;