Stack Overflow

Subscribe to Stack Overflow feed
most recent 30 from stackoverflow.com 2017-11-22T13:16:57Z
Updated: 9 hours 4 min ago

Stripe Order API and Product API

Tue, 11/21/2017 - 00:49

Is it possible to use the Stripe Orders API without using Stripe's payments API? I've managed to create some sample products and create an order and invoke the payment related method with a sample token. This all works, but in a live environment I'm not clear what would happen? The orders and products APIs are very useful but because Stripe can't be used with PayPal, we can't use it as the provider for payments...

How to capture web table to language specific data structure without using external file in python

Tue, 11/21/2017 - 00:49

I am working on Selenium automation. Web application has a web table and few functions on screen alters web table, i.e. merges rows, add two rows, something like that. To perform this operation, I need to capture existing data, perform action and then validate Web application output with my output.

Is there any way I can save web table internally data type, data structure, without using an external file like, txt, excel, csv, etc.

Pandas data frame could be option but don't know how to implement?

The following web crawler is not working: Why is that?

Tue, 11/21/2017 - 00:49
request(url, function (err, res, html) { if (!err) { var $ = cheerio.load(html); $('.wiki-heading-content >p').each(function(){ $(this).each(function(){ var post = {"content": "" }; var data=$(this); post['content']=data.text(); console.log(post); }); }); }

});

var request = require('request'), cheerio = require('cheerio');

var url = "https://namu.wiki/w/크롤링";

Hello I am sorry to have asked you so unpleasantly the other time. I'm trying to create a web crawler with Node.js. The above code is JavaScript, and I am trying to get the code attached below. enter image description here

How to align right all columns except the 1st 2?

Tue, 11/21/2017 - 00:49

I have a table that has 27 columns and I am using fpdf to create pdf file.

I wonder how can I make all the columns align right except the 1st 2?

Here is my code.

#Create the table function BasicTable($header,$data) { #Create the header. foreach ($header as $col) $this->Cell(18,5,$col,1); $this->Ln(); #Get the data foreach ($data as $row) { foreach ($row as $col) #$this->Cell(18,5,$col,1,'R'); $this->Cell(18,5, $col, 1, 0); $this->Ln(); } } }

SQL Server 2012 - Trigger failing to commit data locally despite try-catch for sp_testlinkedserver

Tue, 11/21/2017 - 00:48

If I make a change to MyTable on MyDatabase, I want it to delete the contents of TestTable on TestDatabase on MYLINKEDSERVER and reinsert everything from scratch. To do this, I use a trigger on MyTable. I know this is inefficient but the number of records is less than 10.

In case the linked server is down, I want it to still commit the changes locally to MyTable and just send an email alert stating that the linkedserver could not be updated. I am failing to get the local transaction to commit when the linkedserver is unavailable... I've tried messing with XACT_ABORT but I just get different errors.

What am I doing wrong?

CREATE TRIGGER trig_updatelinkedserver ON MyDatabase.dbo.TestTable FOR INSERT, UPDATE, DELETE AS BEGIN DECLARE @linked_server SYSNAME = 'MYLINKEDSERVER' , @tablename SYSNAME --name of the table calling the trigger so we can send error details in alert eamil SELECT @tablename = OBJECT_NAME(parent_object_id) FROM sys.objects WHERE sys.objects.name = OBJECT_NAME(@@PROCID) BEGIN TRY --If linkedserver fails to connect, we do not want the remaining code in the block to run --but we do want the original transaction that triggered this to complete. EXEC sp_testlinkedserver @servername = @linked_server DELETE FROM MYLINKEDSERVER.TestDatabase.dbo.TestTable INSERT INTO MYLINKEDSERVER.TestDatabase.dbo.TestTable SELECT * FROM MyDatabase.dbo.TestTable END TRY BEGIN CATCH DECLARE @subj VARCHAR(1000) = 'TRIGGER FAILURE: ' + @tablename + ': Could not locate linkedserver ' + @linked_server EXEC msdb.dbo.sp_send_dbmail @recipients = 'foo@bar.com' , @subject = @subj , @body = '' , @body_format = 'HTML' , @profile_name = 'MyEmailProfile' END CATCH END

How to return array of type char *

Tue, 11/21/2017 - 00:36

I am trying to return an array but I am keep getting errors. My code is

char* insert(int *n) { printf("Enter the no of student names to be inserted: "); scanf("%d", n); char *array[*n]; for (int i = 0; i < *n; i++) { array[i] = (char *) malloc(sizeof(50)); scanf("%s", array[i]); } for (int i = 0; i < *n; i++) printf("%s\n", array[i]); } return array; }

RefreshToken undefined after successful Authentication ServiceStack

Tue, 11/21/2017 - 00:35

The following code calls my Auth microservice and successfully authenticates a user and returned there bearer token:

var request = new Authenticate(); request.provider = "credentials"; request.userName = userName; request.password = password; request.useTokenCookie = true; this.client.post(request) .then(res => { if (res.bearerToken != null) { console.log('auth success', res); console.log('auth refresh token', res.refreshToken); resolve(true); } resolve(false); }, msg => { console.log('log in failed'); reject(msg); })

The problem is there is no refresh token return from my Auth microservice:

My configuration for my Auth Microservice:

Plugins.Add(new AuthFeature(() => new CustomUserSession(), new IAuthProvider[] { new JwtAuthProvider { HashAlgorithm = AuthSettings.HashAlgorithm, RequireSecureConnection = requireSecureConnection, AuthKeyBase64 = AuthSettings.JwtAuthKeyBase64, ExpireTokensIn = TimeSpan.FromHours(_configuration["AuthSettings:ExpireTokensIn"].ToDouble()), ExpireRefreshTokensIn = TimeSpan.FromHours(_configuration["AuthSettings:ExpireRefreshTokensIn"].ToDouble()), CreatePayloadFilter = (payload,session) => { payload["zipCode"] = ((CustomUserSession)session).ZipCode; }, PopulateSessionFilter = AuthSettings.PopulateSessionFilterImplementation }, new CustomCredentialsAuthProvider((ITsoContext)_serviceProvider.GetService(typeof(ITsoContext))) //HTML Form post of User/Pass }));

OneToMany ManyToOne removing child object with Hibernate

Tue, 11/21/2017 - 00:29

I'm using Spring Boot and Hibernate. Lets assume we have two entities:

@Entity public class A{ @OneToMany(mappedBy = "objectA",fetch = FetchType.EAGER,cascade = CascadeType.ALL,orphanRemoval=true) private Set<B> objectSet = new HashSet<>(); } @Entity public class B{ @ManyToOne private A objectA; }

And we have two transacional methods;

deleteB_X(int idB){ entityManager.remove(entityManager.find(idB,B.class)); } deleteB_Y(int idB){ B obj=entityManager.find(idB,B.class); obj.getObjectA().getObjectSet().remove(obj); }

What I understand (correct me if I'm wrong):

  1. We have orphanRemoval=true so deleteB_Y(int) will work.
  2. By setting mappedBy argument we say that class A is "the owning site" of relation.
  3. CascadeType is used when we persist/update/merge/remove class A (then it invokes persist/update/merge/remove on child property objectSet). I think we can say that it protects me from situation where I end up with object of B and no object of A class (unless we manually add some B objs).

From what I understand CascadeType should not interfare with orphanRemoval, because CascadeType takes care of things where we do 'some stuff' with A's objects (and then recursively do it to B's objects). And here is something that I don't understand at all. Why deleteB_x(int) doesn't work and why if we remove CascadeType it starts working? I feel like that deleteB_X(int) is much cleaner solution to removing object B from the DB than deleteB_Y(int), but sadly it won't work since it colides with CascadeType.

EDIT1. Method deleteB_X(int) just doesn't remove object from DB, if we remove cascade = CascadeType.ALL evertyhing works just fine. Why? EDIT2. My code (service and then dao)

@Override @Transactional public void deleteB(int id) { B b=dao.getB(id); dao.deleteB(b); } @Override public void deleteB(B b) { entityManager.remove(b); }

Simple method from controller:

@RequestMapping(value = "/test5", method = RequestMethod.GET) @ResponseBody public String test5() { service.deleteB(4); return "OK"; }

Google Apps Script Multiple Search with Drive App

Tue, 11/21/2017 - 00:17

I hope I have searched good enough in order to ask this questions.

I have this code to clean up some default files in our team drive, if somebody got them in sync by accident...

In this case desktop.ini

function cleanup(){ var folderID = 'FOLDERID'; var search = 'title Contains "desktop.ini"'; var fileIDs = []; var files = DriveApp.searchFiles(search); while (files.hasNext()){ files.next().setTrashed(true); } Logger.log('fileIDs.length + ' files have been moved to trash'); }

Is there a way to add multiple hits in one search?

What if I would want to add other files like "thumbs.db" or from the mac guys ".DS_Store". Do I need to search for everything individually or is there a .gitignore way to solve it?

R round PosixCT variable in a data table

Tue, 11/21/2017 - 00:14

The following code creates a 10 row data table with one variable, timeStamp, in POSIXct format.

library(data.table) dt <- data.table(timeStamp = seq( as.POSIXct("2017-07-01 14:51:50"), by=60, len=10))

I want to round timeStamp to the nearest minute.

This command puts a list in each row of timeStamp2 rather than modified POSIXct variables.

dt[, timestamp2 := round(timeStamp, "mins")]

The line of code below does what I want (round up in this example) but doesn't work within the data table.

timestamp2 <- round(dt$timeStamp, "mins")

I'm using data.table version 1.10.4-3 and MRAN R version 3.4.1.

loop (for loop) though even pixels in a picture in jython

Mon, 11/20/2017 - 22:33

I need to use a single for loop to loop through every even pixel in a picture. I think I was getting close with this code, but jython does not like it and I do not know why (something with the second for loop).

for x in range(0, width): for y in range(0, height): px = getPixels(pic, x, y)

Any help would be much appreciated.

C++ Q: How can I get "CLOSE_WAIT" state of specification socket

Mon, 11/20/2017 - 16:49

I'm using VC++ on Visual Studio 2010.
Now I've got some problem. I think it's so silly question but I want to get a clear answer.

How can I get CLOSE_WAIT state of an selected SOCKET?

// SocketThreadConn.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <WinSock.h> #include <Windows.h> #pragma comment(lib, "ws2_32.lib") #define PR_RECORED_TIME 10*1000 // (ms) BYTE* pByteCamData = NULL; INT nHeight = 900; INT nWidth = 1600; INT nSpect = 3; INT nSolution = nHeight * nWidth * nSpect; VOID SendRecoredData(SOCKET socket2operation); int _tmain(int argc, _TCHAR* argv[]) { pByteCamData = new BYTE[nSolution]; // <-- use [], not ()! //---------------------- // Initialize Winsock. WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != NO_ERROR) { wprintf(L"WSAStartup failed with error: %d\n", iResult); delete[] pByteCamData; return 1; } //---------------------- // Create a SOCKET for listening for // incoming connection requests. SOCKET ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ListenSocket == INVALID_SOCKET) { wprintf(L"socket failed with error: %d\n", WSAGetLastError()); WSACleanup(); delete[] pByteCamData; return 1; } //---------------------- // The sockaddr_in structure specifies the address family, // IP address, and port for the socket that is being bound. sockaddr_in service = {}; service.sin_family = AF_INET; service.sin_addr.s_addr = INADDR_ANY; // inet_addr("127.0.0.1"); service.sin_port = htons(27015); if (bind(ListenSocket, (SOCKADDR *) &service, sizeof(service)) == SOCKET_ERROR) { wprintf(L"bind failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); delete[] pByteCamData; return 1; } //---------------------- // Listen for incoming connection requests. // on the created socket if (listen(ListenSocket, 1) == SOCKET_ERROR) { wprintf(L"listen failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); delete[] pByteCamData; return 1; } //---------------------- // Accept the connection. wprintf(L"Waiting for client to connect...\n"); SOCKET AcceptSocket = accept(ListenSocket, NULL, NULL); if (AcceptSocket == INVALID_SOCKET) { wprintf(L"accept failed with error: %d\n", WSAGetLastError()); closesocket(ListenSocket); WSACleanup(); delete[] pByteCamData; return 1; } wprintf(L"Client connected.\n"); SendRecoredData(AcceptSocket); // <-- logic fix! // No longer need client socket closesocket(AcceptSocket); // <-- CLOSE_WAIT fix! // No longer need server socket closesocket(ListenSocket); WSACleanup(); delete[] pByteCamData; return 0; } VOID SendRecoredData(SOCKET socket2operation) { if(IsSocketAlive(socket2operation) == 0) return; INT nCountDown = 5; INT nSentData, nNumToSend; BYTE *pData; do { if (nCountDown == 0) { nCountDown = 5; pData = pByteCamData; nNumToSend = nSolution; while (nNumToSend > 0) <-- send() fix! { nSentData = send(socket2operation, (char*)pData, nNumToSend, 0); if (SOCKET_ERROR == nSentData) { wprintf(L"send failed with error: %d\n", WSAGetLastError()); return; } pData += nSentData; nNumToSend -= nSentData; } wprintf(L"Sent Camera Data OK [%d] Bytes\n", nSolution); } Sleep(PR_RECORED_TIME); --nCountDown; } while (TRUE); } INT IsSocketAlive(SOCKET socket2check) { if (socket2check == INVALID_SOCKET) return FALSE; INT nError_code = -1; INT nError_code_size = sizeof(nError_code); INT nRetValue = getsockopt(socket2check, SOL_SOCKET, SO_ERROR, (CHAR*)&nError_code, &nError_code_size); // if (nRetValue != -1) { // _tprintf(_T("Error getting socket error code : %d \n"), strerror(nRetValue)); } if (nError_code != 0) { _tprintf(_T("Socket error : %d \n"), strerror(nError_code)); } switch (nError_code) { case 0: return TRUE; break; case SOCKET_ERROR: return FALSE; default: return FALSE; } }

A problem is CLOSE_WAIT 'ed' socket is checked as alive socket.
When SocketThreadConn.exe is working on while loop, it doesn't break loop though Listen socket is CLOSE_WAIT.

How can I check accepted socket is ESTABLISHED?

(This code has changed by @Remy Lebeau)

How can I check is a socket is still ESTABLISHED not using send() or GetTcpTable().

Function GetTcpTable() is working well but I have to find socket only using their value(USHORT). There is no field for socket value in MIB_TCPTABLE.

Keras LSTM, batch data structuring differences

Mon, 11/20/2017 - 13:44

I am trying to understand the differences and implications of structuring sequential data for use in the Keras LSTM model. I would like to forecast electricity demand which has a natural daily/weekly demand shape that is driven by temperature and weekday and hour of day, for example. Say I have 1 month's worth of demands and inputs i.e. array shape (30 days * 24 hours of demand, 3 features), and I want to predict the next 30 days of demand based on expected future inputs. What are the implications of the following (particularly with respect to statefulness):

#A. feed in 1 batch of 1 hour at a time. this seems the slowest to train model.add(LSTM(n_neurons, batch_input_shape=(1, 1, 3), stateful=True)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') for i in range(n_epoch): model.fit(X, y, epochs=1, batch_size=1, verbose=1, shuffle=False) model.reset_states() #B. feed in 720 batches of 1 hour at a time #is this the same as A, except I need to forecast 720 hours/timesteps at a time model.add(LSTM(n_neurons, batch_input_shape=(720, 1, 3), stateful=True)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') for i in range(n_epoch): model.fit(X, y, epochs=1, batch_size=720, verbose=1, shuffle=False) model.reset_states() #C. feed in 1 batches of 720 hour timesteps at a time #is this the same as A, except I need to forecast 720 hours/timesteps at a time model.add(LSTM(n_neurons, batch_input_shape=(1, 720, 3), stateful=True)) #probably dont need stateful=True here (?) model.add(Dense(720)) model.compile(loss='mean_squared_error', optimizer='adam') for i in range(n_epoch): model.fit(X, y, epochs=1, batch_size=1, verbose=1, shuffle=False) model.reset_states() #D. some variation so that number_of_batches x timesteps = 720 (no overlap of sequences). #Timesteps most likely in multiples of 24 timesteps representing hours of the day to capture the profile shape model.add(LSTM(n_neurons, batch_input_shape=(number_of_batches, timesteps, 3), stateful=True)) model.add(Dense(timesteps)) model.compile(loss='mean_squared_error', optimizer='adam') for i in range(n_epoch): model.fit(X, y, epochs=1, batch_size=number_of_batches, verbose=1, shuffle=False) model.reset_states()

I've read around so much but still don't quite get the full hang of LSTMs, so any help is much much appreciated! Any recommendations are welcome too.

Deconvoluting a gzipped, binary file containing integer counts into human readable numbers

Mon, 11/20/2017 - 12:52

I need to decode a file gzipped, binary file containing integer counts into human readable numbers so I can summarise them (fx mean, median, etc).

An example of one of the files can be found here (572 bytes)

According to the official documentation it should contain 1001 integer counts (indicating the Fragment length distribution) encoded as "signed, 32-bit integers (with machine-native endianness"- can somebody help me extract them.

python count values in a list

Mon, 11/20/2017 - 12:36

I've got a list with items:

{'2017-11-20': ['GB']} {'2017-11-20': ['GB']} {'2017-11-20': ['ES']} {'2017-11-20': ['ES']} {'2017-11-20': ['ES']} {'2017-11-20': ['DE']} {'2017-11-20': ['DE']} {'2017-11-20': ['DE']} {'2017-11-20': ['DE']} {'2017-11-20': ['DE']} {'2017-11-20': ['DE']} {'2017-11-20': ['CA']} {'2017-11-20': ['CA']} {'2017-11-20': ['CA']} {'2017-11-20': ['CA']}

I am trying to count all values (ES, GB, etc) And print the top 5 occurrence from the date. So should be something like:

2017-11-20: 5x GB 2017-11-20: 4x ES

I can't get it working I have to following code now:

landen.append({datumbestand : [land]}) landensorted = sorted(landen, key=itemgetter(datumbestand), reverse=True) for i in landensorted: print i

Is it possible with a list? Or should I try with a dictionary. I tryed to figure out how to do it with dictionary but I did fail.

scores_unique = {} for item in landensorted: scores_unique[item[datumbestand]] += item[land] sortedlist = sorted(scores_unique.items(), key=lambda x: x[1])

How to add incremental value for the automation id of a Listview label in Xamarin.Forms?

Mon, 11/20/2017 - 12:36

I have my resource like this

<ContentPage.Resources> <ResourceDictionary> <x:String x:Key="LabelAutomationIdentifier">LBL_</x:String> </ResourceDictionary> </ContentPage.Resources>

And my listview looks like

<ListView ItemsSource="{Binding ListItemSource}"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <Label AutomationId="{StaticResource LabelAutomationIdentifier, StringFormat {0}_{Incremental Value OR UniqueIdentifier}'}" Text="{Binding Title}" /> </ViewCell> </DataTemplate> </ListView.ItemTemplate>

How to add the incremental value in the automation id?

Example to control relay module

Mon, 11/20/2017 - 12:36

Does anyone know how to program a device on the android to control this relay module. I do not know about it and I did found a program but only on windows :( ?

enter image description here

Problems submitting posts to LinkedIn via Rest-API and "submitted-image-url"

Mon, 11/20/2017 - 12:36

I have a problem with the image representation of shared posts on LinkedIn. I use a social media marketing tool and although the images are submitted in the correct format (1200x627px), they are sometimes displayed correctly and sometimes too small on LinkedIn.

The posts are shared via the Developer API. The field "submitted-iamge-url" is transferred

how to retrieve text from span & p tag in r

Mon, 11/20/2017 - 12:36

I have following link

url = "https://timesofindia.indiatimes.com/topic/Adani"

In above url I want to extract the headline, para below that and date in 3 different columns.

I am able to extract only one news headline and para with following code

results_headline <- url2 %>% read_html() %>% html_nodes(xpath='//*@id="c_topic_list1_1"]/div[1]/ul/li[4]/div/a/span[1]') results_para <- url2 %>% read_html() %>% html_nodes(xpath='//*[@id="c_topic_list1_1"]/div[1]/ul/li[4]/div/a/p')

I want to extract all the headlines,paragraph and date on that page. How can I do it in R?

transform() to add rows with dplyr()

Mon, 11/20/2017 - 12:36

I've got a data frame (df) with two variables, site and purchase.

I'd like to use dplyr() to group my data by site and purchase, and get the counts and percentages for the grouped data. I'd however also like the tibble to feature rows called ALLSITES, representing the data of all the sites grouped by purchase, so that I end up with a tibble looking similar to dfgoal.

The problem's that my current code doesn't get me the ALLSITES rows. I've tried adding a base R function into dplyr(), which doesn't work.

Any help would be much appreciated.

Starting point (df):

df <- data.frame(site=c("LON","MAD","PAR","MAD","PAR","MAD","PAR","MAD","PAR","LON","MAD","LON","MAD","MAD","MAD"),purchase=c("a1","a2","a1","a1","a1","a1","a1","a1","a1","a2","a1","a2","a1","a2","a1"))

Desired outcome:

dfgoal <- data.frame(site=c("LON","LON","MAD","MAD","PAR","ALLSITES","ALLSITES"),purchase=c("a1","a2","a1","a2","a1","a1","a2"),bin=c(1,2,6,2,4,11,4),pin_per=c(33.33333,66.66667,75.00000,25.00000,100.00000,73.33333,26.66666))

Current code:

library(dplyr) df %>% group_by(site, purchase) %>% summarize(bin = sum(purchase==purchase)) %>% group_by(site) %>% mutate(bin_per = (bin/sum(bin)*100)) df %>% rbind(df, transform(df, site = "ALLSITES") %>% group_by(site, purchase) %>% summarize(bin = sum(purchase==purchase)) %>% group_by(site) %>% mutate(bin_per = (bin/sum(bin)*100))

Pages