Q: API-Platform: Multiple primary key with swagger - getting error This question on GitHub
I'm trying to add second field into parameters without custom controller for entity with multiple primary key and getting error ...
Now step-by-step:
DB table:
create table measure_types
(
    id                varchar(10)  not null,
    language_iso_code varchar(2)   not null,
    label             varchar(255) null,
    created_at        datetime     not null,
    updated_at        datetime     null,
    constraint measure_types_id_language_iso_code_uindex
        unique (id, language_iso_code),
    constraint measure_types_languages_iso_code_fk
        foreign key (language_iso_code) references languages (iso_code)
);

alter table measure_types
    add primary key (id, language_iso_code);

src/Entity/MeasureType.php
namespace Api\Entity;

use DateTime;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Exception;

/**
 * @ORM\Table(
 *     name="measure_types",
 *     indexes={
 *         @ORM\Index(name="measure_types_uindex", columns={"id", "language_iso_code"})
 *     },
 *     uniqueConstraints={
 *         @ORM\UniqueConstraint(name="measure_types_uindex", columns={"id", "language_iso_code"})
 *     }
 * )
 * @ORM\Entity(repositoryClass="Klarstein\Api\Repository\MeasureTypeRepository")
 * @ORM\HasLifecycleCallbacks
 */
class MeasureType
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="string", length=10)
     */
    private string $id;

    /**
     * @ORM\Id()
     * @ORM\ManyToOne(targetEntity="Language", fetch="EAGER")
     * @ORM\JoinColumn(name="language_iso_code", referencedColumnName="iso_code")
     */
    private Language $language;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private string $label;

    /**
     * @ORM\Column(type="datetime")
     */
    private DateTimeInterface $createdAt;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private ?DateTimeInterface $updatedAt;

    public function __construct()
    {
        $this->createdAt = new DateTime();
    }

    /**
     * @ORM\PreUpdate
     *
     * @throws Exception
     */
    public function onPutHandler(): void
    {
        $this->updatedAt = new DateTime();
    }

    // ... under this comment getters and setters
}

config/api/measure_type.yaml
resources:
  Api\Entity\MeasureType:
    properties:
      id:
        identifier: true
      language:
        identifier: true
    attributes:
      pagination_items_per_page: 25
    collectionOperations:
      get:
        normalization_context:
          groups: ['v1_get_collection']
      post:
        normalization_context:
          groups: ['v1_post_collection_response']
        denormalization_context:
          groups: ['v1_post_collection_request']
    itemOperations:
      get:
        method: 'GET'
        normalization_context:
          groups: ['v1_get_item']
      put:
        normalization_context:
          groups: ['v1_put_item_response']
        denormalization_context:
          groups: ['v1_put_item_request']
      delete:
        denormalization_context:
          groups: ['v1_delete_item']

It's work, for current moment:

But i want to show in doc, that, I expect for 2 parameters as identifier.
What I'm doing:
Adding: path: '/measure_types/id={id};language={language}' to config/api/measure_type.yaml
resources:
  Api\Entity\MeasureType:
    properties:
      id:
        identifier: true
      language:
        identifier: true
    attributes:
      pagination_items_per_page: 25
    collectionOperations:
      get:
        normalization_context:
          groups: ['v1_get_collection']
      post:
        normalization_context:
          groups: ['v1_post_collection_response']
        denormalization_context:
          groups: ['v1_post_collection_request']
    itemOperations:
      get:
        method: 'GET'
        path: '/measure_types/id={id};language={language}'
        normalization_context:
          groups: ['v1_get_item']
      put:
        normalization_context:
          groups: ['v1_put_item_response']
        denormalization_context:
          groups: ['v1_put_item_request']
      delete:
        denormalization_context:
          groups: ['v1_delete_item']

Next step: I have SwaggerEventRequire Decorator:
src/Swagger/SwaggerEventRequireDecorator.php
namespace Api\Swagger;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Yaml\Yaml;

final class SwaggerEventRequireDecorator implements NormalizerInterface
{
    private const SWAGGER_DECORATIONS = __DIR__ . '/../../config/swagger/';

    private NormalizerInterface $decorated;

    public function __construct(NormalizerInterface $decorated)
    {
        $this->decorated = $decorated;
    }

    public function normalize($object, string $format = null, array $context = [])
    {
        $docs = $this->decorated->normalize($object, $format, $context);

        $customDefinition = $this->loadDefinitions();

        foreach ($customDefinition as $path => $methods) {
            foreach ($methods as $method => $parameters) {
                foreach ($parameters as $paramKey => $paramValues) {
                    if (empty($paramValues['name'])) {
                        continue;
                    }

                    if (empty($docs['paths'][$path]) || empty($docs['paths'][$path][$method])) {
                        continue;
                    }

                    // e.g. remove an existing event parameter
                    $docs['paths'][$path][$method]['parameters'] = array_values(
                        array_filter(
                            $docs['paths'][$path][$method]['parameters'],
                            function ($param) use ($paramKey) {
                                return $param['name'] !== $paramKey;
                            }
                        )
                    );

                    // e.g. add the new definition for event
                    $docs['paths'][$path][$method]['parameters'][] = $paramValues;
                }
            }
        }

        return $docs;
    }

    public function supportsNormalization($data, string $format = null)
    {
        return $this->decorated->supportsNormalization($data, $format);
    }

    private function loadDefinitions(): array
    {
        $result = [];
        $finder = new Finder();
        $finder
            ->files()
            ->in(self::SWAGGER_DECORATIONS)
            ->name('*.yaml');

        foreach ($finder as $file) {
            $yaml = Yaml::parseFile(self::SWAGGER_DECORATIONS . $file->getFilename());
            if (empty($yaml)) {
                continue;
            }
            $result = array_unique(array_merge($result, $yaml), SORT_REGULAR);
        }

        return $result;
    }
}

and decoration config file: config/swagger/measure_type.yaml
/measure_types/id={id};language={language}:
  get:
    id:
      name: 'id'
      description: 'Measure type ID'
      in: 'path'
      required: true
      type: 'string'
      example: 'ml'
    language:
      name: 'language'
      description: 'Language ISO code'
      in: 'path'
      required: true
      type: 'string'
      example: 'de'

In a result, I got working form in doc, but wrong result:

I want to use it without custom controller, because I have many entities with multiple primary keys and adding controllers for each one will take a lot of time
What I do wrong?

A: Solved. The solution is on github gave be Kevin.


Q: EF UpdateRange() not working with deleted child collections public Class Parent
{
    public int Id {get;set;}
    public string Name {get;set;}
    public ICollection<Child> Children {get;set;}
}

public Class Child
{
    public int Id {get;set;}
    public int ParentId {get;set;}
    public string Name {get;set;}
}

Say that I have 1 Parent, and the Parent has 2 Children.
Parent
-- Id = 1
-- Name = "Mark"

Child 1
-- Id = 1
-- ParentId = 1
-- Name = "Tom"

Child 2
-- Id = 2
-- ParentId = 1
-- Name = "Jack"

After making the following changes (Deleted Jack and renamed Tom to Luffy:
Parent
-- Id = 1
-- Name = "Mark"

Child 1
-- Id = 1
-- ParentId = 1
-- Name = "Luffy"

Child 2 - Deleted

Calling DbContext.ParentDbSet.UpdateRange(Mark) will see that Tom is successfully renamed into Luffy, but Jack remained undeleted.
Parent
-- Id = 1
-- Name = "Mark"

Child 1
-- Id = 1
-- ParentId = 1
-- Name = "Luffy"

Child 2
-- Id = 2
-- ParentId = 1
-- Name = "Jack"

I get get around this by calling DbContext.ChildDbSet.Remove(Jack) before calling SaveChanges.
However, I am trying to avoid this because by the time I call DbContext.ParentDbSet.UpdateRange(Mark), Jack is already removed from Mark.
Currently I get the old Mark again from the database, compare it with the updated Mark (the one with Jack removed) to find out the deleted Child records, and then call RemoveRange(The deleted Child records).
This doesn't seem like a good idea...
In brief, I want to achieve the followings before calling SaveChanges():

*

*Update Parent

*Update Parent's Child records that have been changed

*Delete Parent's Child records that have been deleted

*Insert Parent's Child records that have been added (p.s. UpdateRange() seems to work with inserting child collections too)

Is there a better way to do this?
Thanks!


Q: Is it possible to work with a spring java project without internet necessity? Hello brothers in code.
Always when I try to work with no internet connection (some times this goes down in my network) I do a question to myself... "There's a way to configure a spring maven java project without internet connection necessity?"... So, since the first time that this doubt come to me I'm searching this answer in Google and never found it. What you all, Jedis of the coding, could say to me about this?
Since now I'm very thankful for the help

A: This is a general question, so I'll try to provide a general answer
In a nutshell, Spring itself does not require an internet connection at runtime in a sense that it is not supposed to contain code that goes "somewhere on the internet" and queries for something.
However, Spring has a lot of dependencies (actually just like your own project probably has dependencies) so that Maven will have to bring them from somewhere upon the first run.
So Maven (that you've mentioned as a build tool) by default will require an internet connection.  Of course, there are many options to overcome this "difficulty" all of them boil down to making all these dependencies available so that you'll be able to compile the project without going to the internet.
The actual solution can vary:

*

*List item "install Nexus/Artifactory" that will act as a proxy and will download dependencies for you. It makes sense if your network infrastructure has an option to connect to the internet from some servers leaving your "developed machine" connected only to the internal network.

*Download the whole Maven repository with some crawler (it exposes web interface) to your machine and use it there (if you work for organization that doesn't have any kind of internet connection)

*Just come to the place that has an internet connection with your PC, compile everything once, Maven will download all the dependencies and cache them in your local m2 repository. So next time you'll be able to build your project even without internet connection.
I know the last option sounds more like a joke, but it also technically works if you, say a student that doesn't have any connection at home but wants to try this "Spring thing" out :)

A: You can find some more information about mavens offline flags in this post f.e.: Is there a maven command line option for offline mode?


Q: Should TypeScript's mapped types have a value for every key? I am wondering whether every key in a mapped type should have a defined value.
I would not expect this to be the case as it would require indefeasibly many values. However, then I'd expect Partial<T> to be assignable to a mapped type T, which it is not. Additionally, when reading a value from a mapped type, no typeof value === 'undefined' check is necessary.

type Mapped = { [key: string]: string };

// This works, so not all keys have to be defined
const mapped: Mapped = { 'a': 'b' };

// This is valid even with strict checks even though 's' is undefined
const s: string = mapped['c'];

const partialMapped: Partial<Mapped> = mapped;
// This doesn't work because `Partial<Mapped>` is of type `{ [key: string]: string | undefined }`
const secondMapped: Mapped = partialMapped;

Since { 'a': 'b' } is assignable to a Mapped variable, I would expect Partial<Mapped> to be assignable to Mapped as well, but is is not. Is this because the value undefined is not the same as the absence of a key? Is there a variant on Partial that makes keys optional instead?

A: Your code works with strict null checks off, because you can then assign undefined values to a string. We're talking about values here, the keys are indifferent!
Your index type signature for Mapped promises no particular keys, so assigning a type with zero or more keys will work. What isn't allowed is the assignment of non-string values (like undefined and null) and this is what the error is highlighting.
Here is a contrived example of what the error is guarding against:
var undef: undefined;
const partialMapped: Partial<Mapped> = {a: undef, b: undef}; // Works

The partial version can accept undefined values. The "full" version wouldn't allow this.
var undef: undefined;
const mapped: Mapped = {a: undef, b: undef}; // Warnings!


Q: Display different login page based on requirements in Phonegap android apps? My Phonegap application login page contains a few input fields like username, password, url, device type, etc... that the user has to enter to activate the application when he logins for the fist time. The next time on the user has to enter only the username and password as the application is already activated. So I want to display only the username and password fields the second time on login.
I have a phonegap plugin to check whether the app is activated or not. I want to call this plugin and based on the callback response I will show only necessary fields on the login page.So what would be the easiest way to do this?
I tried this..
$("#loginPage").live('pagebeforeshow',function(event, ui){
window.plugins.AuthPlugin.appIsActive(appIsActiveCallBack);
});
function appIsActiveCallBack(result){//Show only relative fields...}

But I got this error:Uncaught TypeError: Cannot call method 'appIsActive' of undefined
Thanks in advance.

A: You are getting Uncaught TypeError because you are trying to call the plugin before even the device is ready... Call the Plugin only when the device is ready...
UPDATE
What you have to do is to determine what all fields you need to show in native side only...
After that pass a variable(flag) from java to JavaScript which will be accessible at pagebeforeshow
So your Activity will look like something like this
/*
 * Some database operations which you need to check
 *  so that you can determine what to show
 */
this.setIntegerProperty("loadUrlTimeoutValue", 70000);
super.loadUrl("file:///android_asset/www/index.html", 20000);
super.loadUrl("javascript: { var pageFlag = '" + flag + "';}");

And in your index.html show like this
$("#loginPage").live('pagebeforeshow',function(event, ui){
alert(pageFlag);
});

After that with the use of flag you can determine what to show and what not to...
Hope it helps...


Q: multiple definition in header file Given this code sample:
complex.h :
#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>

class Complex
{
public:
   Complex(float Real, float Imaginary);

   float real() const { return m_Real; };

private:
   friend std::ostream& operator<<(std::ostream& o, const Complex& Cplx);

   float m_Real;
   float m_Imaginary;
};

std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
   return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
#endif // COMPLEX_H

complex.cpp :
#include "complex.h"

Complex::Complex(float Real, float Imaginary) {
   m_Real = Real;
   m_Imaginary = Imaginary;
}

main.cpp :
#include "complex.h"
#include <iostream>

int main()
{
   Complex Foo(3.4, 4.5);
   std::cout << Foo << "\n";
   return 0;
}

When compiling this code, I get the following error:
multiple definition of operator<<(std::ostream&, Complex const&)

I've found that making this function inline solves the problem, but I don't understand why. Why does the compiler complain about multiple definition? My header file is guarded (with #define COMPLEX_H).
And, if complaining about the operator<< function, why not complain about the public real() function, which is defined in the header as well?
And is there another solution besides using the inline keyword?

A: The problem is that the following piece of code is a definition, not a declaration:
std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
   return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}

You can either mark the function above and make it "inline" so that  multiple translation units may define it:
inline std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
   return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}

Or you can simply move the original definition of the function to the "complex.cpp" source file.
The compiler does not complain about "real()" because it is implicitly inlined (any member function whose body is given in the class declaration is interpreted as if it had been declared "inline"). The preprocessor guards prevent your header from being included more than once from a single translation unit ("*.cpp" source file"). However, both translation units see the same header file. Basically, the compiler compiles "main.cpp" to "main.o" (including any definitions given in the headers included by "main.cpp"), and the compiler separately compiles "complex.cpp" to "complex.o" (including any definitions given in the headers included by "complex.cpp"). Then the linker merges "main.o" and "complex.o" into a single binary file; it is at this point that the linker finds two definitions for a function of the same name. It is also at this point that the linker attempts to resolve external references (e.g. "main.o" refers to "Complex::Complex" but does not have a definition for that function... the linker locates the definition from "complex.o", and resolves that reference).

A: Move implementation to complex.cpp
Right now after including this file implementation is being compiled to every file.
Later during linking there's a obvious conflict because of duplicate implementations.
::real() is not reported because it's inline implicitly (implementation inside class definition)

A: I was having this problem, even after my source and header file were correct.
It turned out Eclipse was using stale artifacts from a previous (failed) build.
To fix, use Project > Clean then rebuild.

A: An alternative to designating a function definition in a header file as inline is to define it as static. This will also avoid the multiple definition error.


Q: Django Rest Framework - React - Unable to login immediately after logout This is very strange. I have a react front end and a django backend with djangorestframework and django-allauth for authentication. Everything works fine from Postman. But in the browser when I successfully sign in, successfully sign out, and then try to sign in again, I get a 401 unauthorized error. The correct user credentials are sent to the server just as the first successful time, yet a 401 error. However, after I refresh the browser then I am able to sign in normally again. I use jwt for authentication and I append the token to the authorization header before sign out. I even tried clearing out the authorization header after a successful sign out but to no avail. It is the same problem with my react native front end. I don't know if this is a react or a django problem. Please does anyone have any idea what the problem might be? Thanks.

A: The problem solved! I appended the Token to the authorization header like so:
request.headers['Authorization'] = Token ${token}
Except when signing out, every other request did not require the authorization header to be set as above. So after sign out, the authorization header becomes:
request.headers.Authorization = Token null
That null value of the token would make every request after sign out "Unauthorized". So to solve this, I had to set the Authorization header for every request when there is token and then delete the Authorization from the header object when there is no token like so:
delete request.headers.Authorization


Q: Inject service in app.config? I want to inject a service into app.config any idea please ?I want to inject a service into app.config any idea please ?I want to inject a service into app.config any idea please ?
app.js
  'use strict';

angular.module('crud', [
  'ngRoute',
  'angular-jwt',
  'ngSails',
  'ngMessages',
  'ngResource'

])
  .config(function ($httpProvider,$routeProvider, $locationProvider,$sailsProvider,jwtInterceptorProvider,User) {

  //$httpProvider.interceptors.push('jwtInterceptor');

     //console.log($sailsProvider);
    $routeProvider
      .otherwise({
        redirectTo: '/'
      });

    $locationProvider.html5Mode(true);

  });

Serviceuser.js
'use strict';

angular.module('crud').service('User', function ($sails) {

    //console.log($sails);
    return {

        signup:  function (data) {
            return $sails.post('/api/user',data);
        }
    }

});

A: Here you go straight from the docs:
Registering a Service with $provide
You can also register services via the $provide service inside of a module 's config function:

angular
  .module('myModule', [])
  .config(['$provide ',
    function($provide) {
      $provide.factory('serviceId ', function() {
        var shinyNewServiceInstance;
        // factory function body that constructs shinyNewServiceInstance
        return shinyNewServiceInstance;
      });
    }
  ]);

This technique is often used in unit tests to mock out a service'
s dependencies.

Hope this helps.
(function() {
  'use strict';

  angular
    .module('example.app', [])
    .config(['$provide',
      function($provide) {
        $provide.factory('serviceId', function() {
          var shinyNewServiceInstance;
          // factory function body that constructs shinyNewServiceInstance
          return shinyNewServiceInstance;
        });
      }
    ])
    .controller('ExampleController', ExampleController)
    .service('exampleService', exampleService);

  exampleService.$inject = ['$http'];

  function ExampleController(exampleService) {
    var vm = this;

    vm.update = function(person, index) {
      exampleService.updatePeople(person).then(function(response) {
        vm.persons = response;
      }, function(reason) {
        console.log(reason);
      });
    };
  }

  // good practice to use uppercase variable for URL, to denote constant.
  //this part should be done in a service

  function exampleService($http) {
    var URL = 'https://beta.test.com/auth/authenticate/',
      data = {},
      service = {
        updatePeople: updatePeople
      };

    return service;

    function updatePeople(person) {
      //person would be update of person.
      return $http
        .post(URL, person)
        .then(function(response) {
          return response.data;
        }, function(response) {
          return response;
        });
    }
  }
})();

A: you can use like:
angular.module('app', ["ui.router"])
.config(function config ($stateProvider){
    $stateProvider.state("index", {
      url:"",
      controller: "FirstCtrl as first",
      templateUrl: "first.html"
    })
    .state("second", {
      url:"/second",
      controller:"SecondCtrl as second",
      templateuRL:"second.html"
    })
})

here is the full working example with plunker


Q: python/Scipy Interpolation of 3D scattered data I want to implement a interpolation of a 3D scattered data using python.
Such as,
z[0,0] = 1, z[0,4] =2, z[2,2]=4, z[4,0]=3, z[4,4]= 2 (X like structured 2D vectors with z value)
and interpolate these data so that
I can derive a z value between this (x,y) coordinate like z[1,2], z[2,3], z[1,1], ..., etc.
I coded like below using scipy and numpy but I guess using numpy.empty((5,5)) is not suitable for my purpose because numpy.empty() add values which is irrelevant. And I want to interpolate only with 5 points.
from scipy import interpolate
import numpy as np
x = np.linspace(0,4,5)
y = np.linspace(0,4,5)
xx,yy=np.meshgrid(x,y)
z = np.empty((5,5)) # I think this is wrong
z[0,0] = 1, z[0,4] =2, z[2,2]=4, z[4,0]=3, z[4,4]= 2

f = interpolate.interp2d(x,y,z,kind='linear')

xt = np.linspace(0,4,10)
yt = np.linspace(0,4,10)
xxt, yyt = np.meshgrid(xt,yt)
ct = f(xt,yt)

my expected output is values with 2D 5x5 (or larger area such as 7x7, 9x9, etc) arrays which is the result of interpolation of 5 constant points.
Could you help me?


Q: Appending data to a Google Sheet using Python I have 3 different tables I'm looking to directly push to 3 separate tabs in a Google Sheet. I set up the GSpread connection and that's working well. I started to adjust my first print statement into what I thought would append the information to Tab A (waveData), but no luck.
I'm looking to append the information to the FIRST blank row in a tab.  Basically, so that the data will be ADDED to what is already in there.
I'm trying to use append_rows to do this, but am hitting a "gspread.exceptions.APIError: {'code': 400, 'message': 'Invalid value at 'data.values' (type.googleapis.com/google.protobuf.ListValue).
I'm really new to this, just thought it would be a fun project to evaluate wave sizes in NJ across all major surf spots, but really in over my head (no pun intended).
Any thoughts?
import requests
import pandas as pd
import gspread
gc = gspread.service_account(filename='creds.json')
sh = gc.open_by_key('152qSpr-4nK9V5uHOiYOWTWUx4ojjVNZMdSmFYov-n50')
waveData = sh.get_worksheet(0)
tideData = sh.get_worksheet(1)
lightData = sh.get_worksheet(2)
# AddValue = ["Test", 25, "Test2"]
# lightData.insert_row(AddValue, 3)

id_list = [
    '/Belmar-Surf-Report/3683/',
    '/Manasquan-Surf-Report/386/',
    '/Ocean-Grove-Surf-Report/7945/',
    '/Asbury-Park-Surf-Report/857/',
    '/Avon-Surf-Report/4050/',
    '/Bay-Head-Surf-Report/4951/',
    '/Belmar-Surf-Report/3683/',
    '/Boardwalk-Surf-Report/9183/',

]

for x in id_list:
    waveData.append_rows(pd.read_html(requests.get('http://magicseaweed.com' + x).text)
                         [2].iloc[:9, [0, 1, 2, 3, 4, 6, 7, 12, 15]].to_json(), value_input_option="USER_ENTERED")
    # print(pd.read_html(requests.get('http://magicseaweed.com' + x).text)[0])
    # print(pd.read_html(requests.get('http://magicseaweed.com' + x).text)[1])

A: From your following reply,

there really is no relationship between the 3. When I scrape with IMPORTHTML into Google sheets, those are just Tables at the locations 0,1, and 2. I'm basically just trying to have an output of each table on a separate tab

I understood that you wanted to retrieve the values with pd.read_html(requests.get('http://magicseaweed.com' + x).text)[2].iloc[:9, [0, 1, 2, 3, 4, 6, 7, 12, 15]] from id_list, and wanted to put the values to a sheet in Google Spreadsheet.
In this case, how about the following modification?
At append_rows, it seems that JSON data cannot be directly used. In this case, it is required to use a 2-dimensional array. And, I'm worried about the value of NaN in the datafarame. When these points are reflected in your script, how about the following modification?
Modified script 1:
In this sample, all values are put into a sheet.
gc = gspread.service_account(filename='creds.json')
sh = gc.open_by_key('152qSpr-4nK9V5uHOiYOWTWUx4ojjVNZMdSmFYov-n50')
waveData = sh.get_worksheet(0)

id_list = [
    "/Belmar-Surf-Report/3683/",
    "/Manasquan-Surf-Report/386/",
    "/Ocean-Grove-Surf-Report/7945/",
    "/Asbury-Park-Surf-Report/857/",
    "/Avon-Surf-Report/4050/",
    "/Bay-Head-Surf-Report/4951/",
    "/Belmar-Surf-Report/3683/",
    "/Boardwalk-Surf-Report/9183/",
]

# I modified the below script.
res = []
for x in id_list:
    df = pd.read_html(requests.get("http://magicseaweed.com" + x).text)[2].iloc[:9, [0, 1, 2, 3, 4, 6, 7, 12, 15]].fillna("")
    values = [[x], df.columns.values.tolist(), *df.values.tolist()]
    res.extend(values)
    res.append([])

waveData.append_rows(res, value_input_option="USER_ENTERED")

*

*When this script is run, the retrieved values are put into the 1st sheet as follows. In this sample modification, the path and a blank row are inserted between each data. Please modify this for your actual situation.

Modified script 2:
In this sample, each value is put into each sheet.
gc = gspread.service_account(filename='creds.json')
sh = gc.open_by_key('152qSpr-4nK9V5uHOiYOWTWUx4ojjVNZMdSmFYov-n50')

id_list = [
    "/Belmar-Surf-Report/3683/",
    "/Manasquan-Surf-Report/386/",
    "/Ocean-Grove-Surf-Report/7945/",
    "/Asbury-Park-Surf-Report/857/",
    "/Avon-Surf-Report/4050/",
    "/Bay-Head-Surf-Report/4951/",
    "/Belmar-Surf-Report/3683/",
    "/Boardwalk-Surf-Report/9183/",
]

obj = {e.title: e for e in sh.worksheets()}
for e in id_list:
    if e not in obj:
        obj[e] = sh.add_worksheet(title=e, rows="1000", cols="26")

for x in id_list:
    df = pd.read_html(requests.get("http://magicseaweed.com" + x).text)[2].iloc[:9, [0, 1, 2, 3, 4, 6, 7, 12, 15]].fillna("")
    values = [df.columns.values.tolist(), *df.values.tolist()]
    obj[x].append_rows(values, value_input_option="USER_ENTERED")

*

*When this script is run, the sheets are checked and created with the sheet names of the values in id_list, and each value is put to each sheet.

Reference:

*

*append_rows


Q: Quickest way to delete text from a string in python based on a list? Maybe regex? Hi I have a very long string that has the following structure:
"IF (
ISFILTERED ( Table1[Column_1] ),
VAR ___f = FILTERS ( Table1[Column_1] )
VAR ___r = COUNTROWS ( ___f )
VAR ___t = TOPN ( MaxFilters, ___f, Table1[Column_1])
VAR ___d = CONCATENATEX ( ___t, Table1[Column_1], ", " )
VAR ___x = "Table1[Column_1] = " & ___d & IF(___r > MaxFilters, ", ... [" & ___r & " items selected]") & " "
RETURN ___x & UNICHAR(13) & UNICHAR(10)
)
& IF (
ISFILTERED ( Table1[Column_2] ),
VAR ___f = FILTERS ( Table1[Column_2] )
VAR ___r = COUNTROWS ( ___f )
VAR ___t = TOPN ( MaxFilters, ___f, Table1[Column_2])
VAR ___d = CONCATENATEX ( ___t, Table1[Columnw_1], ", " )
VAR ___x = "Table1[Column_2] = " & ___d & IF(___r > MaxFilters, ", ... [" & ___r & " items selected]") & " "
RETURN ___x & UNICHAR(13) & UNICHAR(10)
)
& IF (
...

It basically continues like that iterating through each column of each table in a schema. As you can imagine it this string will be very large for a lot of tables/columns.
I have a list of columns from specific tables like this:
['Table1[Column_1]', 'Table2[Column_4]', 'Table6[Column_22]']

These are the only columns I am interested in keeping in the string.
So I need to go through the string and remove the entire IF statement it relates too if the table/column is not in the list.
So based on the above example the expected output would be:
    "IF (
ISFILTERED ( Table1[Column_1] ),
VAR ___f = FILTERS ( Table1[Column_1] )
VAR ___r = COUNTROWS ( ___f )
VAR ___t = TOPN ( MaxFilters, ___f, Table1[Column_1])
VAR ___d = CONCATENATEX ( ___t, Table1[Column_1], ", " )
VAR ___x = "Table1[Column_1] = " & ___d & IF(___r > MaxFilters, ", ... [" & ___r & " items selected]") & " "
RETURN ___x & UNICHAR(13) & UNICHAR(10)
)
& IF (
...

we just got rid of the second IF because Table1[Column_2] was not in the list.
Would regex be useful for this case? Or maybe I should iterate through the list and build a new string that just keeps the relevant parts.
I know it is best practise to show what you have attempted so far but I am not sure where to start with this it seems like it should be easy but I am having trouble.
Can anyone help me please?
Python solutions would be best as I know that more, but happy to investigate other methods if easier. I know there are regex tools online maybe I can just use one of those?

A: Assuming string structure is constant. You can try this, but it depends on structure.
data = YOUR_STRING_FROM_QUESTION

# This is the delimeter, which will help us to split query on parts
prefix = '& IF (\n'

# define list of allowed tables
allowed_tables = ['Table1[Column_1]', 'Table2[Column_4]', 'Table6[Column_22]']

# split full string on parts by prefix
query_by_part = data.split(prefix)

# build clean list of query parts, where tables in allowed_tables
clean_query_parts = [part for part in query_by_part
                     if any(table in part for table in allowed_tables)]

# finally join list to string using prefix and print
print(prefix.join(clean_query_parts))

Input:
IF (
ISFILTERED ( Table1[Column_1] ),
VAR ___f = FILTERS ( Table1[Column_1] )
VAR ___r = COUNTROWS ( ___f )
VAR ___t = TOPN ( MaxFilters, ___f, Table1[Column_1])
VAR ___d = CONCATENATEX ( ___t, Table1[Column_1], ", " )
VAR ___x = "Table1[Column_1] = " & ___d & IF(___r > MaxFilters, ", ... [" & ___r & " items selected]") & " "
RETURN ___x & UNICHAR(13) & UNICHAR(10)
)
& IF (
ISFILTERED ( Table1[Column_2] ),
VAR ___f = FILTERS ( Table1[Column_2] )
VAR ___r = COUNTROWS ( ___f )
VAR ___t = TOPN ( MaxFilters, ___f, Table1[Column_2])
VAR ___d = CONCATENATEX ( ___t, Table1[Columnw_1], ", " )
VAR ___x = "Table1[Column_2] = " & ___d & IF(___r > MaxFilters, ", ... [" & ___r & " items selected]") & " "
RETURN ___x & UNICHAR(13) & UNICHAR(10)
)

Output:
IF (
ISFILTERED ( Table1[Column_1] ),
VAR ___f = FILTERS ( Table1[Column_1] )
VAR ___r = COUNTROWS ( ___f )
VAR ___t = TOPN ( MaxFilters, ___f, Table1[Column_1])
VAR ___d = CONCATENATEX ( ___t, Table1[Column_1], ", " )
VAR ___x = "Table1[Column_1] = " & ___d & IF(___r > MaxFilters, ", ... [" & ___r & " items selected]") & " "
RETURN ___x & UNICHAR(13) & UNICHAR(10)
)

A: You can use jinja2 to easily generate the string from scratch in this way:

*

*Add a new file with the double extension 'txt.jinja', e.g., example.txt.jinja2 with the following code inside (this is your template) and save it in the same path of your script:

"
{%- for column in columns_to_add -%}
IF (
ISFILTERED ( Table1[Column_2] ),
VAR ___f = FILTERS ( Table1[Column_2] )
VAR ___r = COUNTROWS ( ___f )
VAR ___t = TOPN ( MaxFilters, ___f, Table1[Column_2])
VAR ___d = CONCATENATEX ( ___t, Table1[Columnw_1], ", " )
VAR ___x = "{{column}} = " & ___d & IF(___r > MaxFilters, ", ... [" & ___r & " items selected]") & " "
RETURN ___x & UNICHAR(13) & UNICHAR(10)
)
&
{% endfor -%}
"

*Execute this Python script:

import os
import jinja2

TEMPLATE_NAME = "example.txt.jinja2"
LIST_OF_TABLES = [
    'Table1[Column_1]',
    'Table2[Column_4]',
    'Table6[Column_22]'
]

template = os.path.join(os.path.dirname(os.path.abspath(__file__)), "")
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(template))
template2= jinja_env.get_template(TEMPLATE_NAME)
string = template2.render(columns_to_add=LIST_OF_TABLES)
output = template + TEMPLATE_NAME.split("\\")[-1].replace('.jinja2', '')
with open(output, "w+") as f:
    f.write(string)

This will create a new file (in the same path) called example.txt with your string:
"IF (
ISFILTERED ( Table1[Column_2] ),
VAR ___f = FILTERS ( Table1[Column_2] )
VAR ___r = COUNTROWS ( ___f )
VAR ___t = TOPN ( MaxFilters, ___f, Table1[Column_2])
VAR ___d = CONCATENATEX ( ___t, Table1[Columnw_1], ", " )
VAR ___x = "Table1[Column_1] = " & ___d & IF(___r > MaxFilters, ", ... [" & ___r & " items selected]") & " "
RETURN ___x & UNICHAR(13) & UNICHAR(10)
)
&
IF (
ISFILTERED ( Table1[Column_2] ),
VAR ___f = FILTERS ( Table1[Column_2] )
VAR ___r = COUNTROWS ( ___f )
VAR ___t = TOPN ( MaxFilters, ___f, Table1[Column_2])
VAR ___d = CONCATENATEX ( ___t, Table1[Columnw_1], ", " )
VAR ___x = "Table2[Column_4] = " & ___d & IF(___r > MaxFilters, ", ... [" & ___r & " items selected]") & " "
RETURN ___x & UNICHAR(13) & UNICHAR(10)
)
&
IF (
ISFILTERED ( Table1[Column_2] ),
VAR ___f = FILTERS ( Table1[Column_2] )
VAR ___r = COUNTROWS ( ___f )
VAR ___t = TOPN ( MaxFilters, ___f, Table1[Column_2])
VAR ___d = CONCATENATEX ( ___t, Table1[Columnw_1], ", " )
VAR ___x = "Table6[Column_22] = " & ___d & IF(___r > MaxFilters, ", ... [" & ___r & " items selected]") & " "
RETURN ___x & UNICHAR(13) & UNICHAR(10)
)
&
"

This works fine no matter how big your list of desired columns is, and the best part is that you only have to keep this list updated.


Q: Download in CSV File using PHP I am trying to convert MySQL table to csv and download it from server. i have uploaded this script on server.
 <?php
  //export.php
 require 'db_connection.php';

 header('Content-Type: text/csv; charset=utf-8');
 header('Content-Disposition: attachment; filename=data.csv');
 $output = fopen("php://output", "w");
 $sql = "select * from  tablaregistro";
 $stmt= $pdo->prepare($sql);
 $stmt->execute();
 while($row = $stmt->fetch())
 {
    fputcsv($output, $row);
 }

 fclose($output);
 ?>

This script is converting table to CSV and file is being downloaded in "data.csv" but when I upload this script to server, it is just displaying CSV on the whole page. It is not downloading like in localhost. I hope my question is clear.


Q: how to update data in server database in same field in iphone I am updating my data but it not updating why it happen
i want if any register user want to change data old to new then i give a update page where the user change there data but except username i try lot byt it's not updata data to server databasetable  my cod is proper or wrong
 -(void)sendRequest
    {
        NSString *post = [NSString stringWithFormat:@"firstname=%@&lastname=%@&Username=%@&Password=%@&Email=%@",txtfirstName.text,txtlast.text,txtUserName.text,txtPassword.text,txtEmail.text];
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
        NSLog(@"%@",postLength);
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:@"http://192.168.0.1:96/JourneyMapperAPI?RequestType=Register&Command=SET"]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        if (theConnection) {
            webData = [[NSMutableData data] retain];
            NSLog(@"%@",webData);
        }
        else
        {

        }

    }

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        [webData setLength: 0];
    }

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [webData appendData:data];

    }

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        [connection release];
        [webData release];
    }

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
        NSLog(@"%@",loginStatus);

        }

A: you need to start the connection:
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (theConnection) {
        webData = [[NSMutableData data] retain];
        NSLog(@"%@",webData);
        [theConnection start];
    }
    else
    {

    }


Q: Chrome Extensions "chrome.storage.local" data updating trouble I am working on one chrome extension and i need to use local storage to send data from options page to background scritps.
Options page script:
function addToStorage(key, val){
    let obj = {};
    obj[key] = val;

    chrome.storage.local.set( obj, function() {
      if(chrome.runtime.lastError) {
        console.error(
          "Error setting " + key + " to " + JSON.stringify(val) +
          ": " + chrome.runtime.lastError.message
        );
      }
    });
}

Background:
chrome.storage.local.get('code', function(code) {
    ... with code.code ...
});

For example:
Now chrome.storage.local code value is abcd
I'm performing addToStorage('code', '1234') from options page script
After that in background script value code only will change when i manually click "update" at chrome extesions page
How can i automatically get actual data at background script?

A: the background script will check only once when started as is.
You could pass a mesage from the options script to background scripts after you update the local storage and use that as a trigger to check storage.
try this:
Options page
function addToStorage(key, val){
    let obj = {};
    obj[key] = val;

    chrome.storage.local.set( obj, function() {
      if(chrome.runtime.lastError) {
        console.error(
          "Error setting " + key + " to " + JSON.stringify(val) +
          ": " + chrome.runtime.lastError.message
        );
      }
      chrome.runtime.sendMessage({status: "Storage Updated"}, function (responce) {
          console.log(responce);
      })
    });
}

Background Page:
chrome.runtime.onMessage.addListener(
    function (request, sender, responce) {
        if (request.status === "Storage Updated") {
            chrome.storage.local.get('code', function(code) {
                // ... with code.code ...
            });

            sendResponce({status: "Update Recieved"});
        }
    }
);

Hope that helps, message passing docs here: https://developer.chrome.com/extensions/messaging


Q: Why do R and statsmodels give slightly different ANOVA results? Using a small R sample dataset and the ANOVA example from statsmodels, the degrees of freedom for one of the variables are reported differently, & the F-values results are also slightly different. Perhaps they have slightly different default approaches? Can I set up statsmodels to use R's defaults?
import pandas as pd
import statsmodels.api as sm
from statsmodels.formula.api import ols

##R code on R sample dataset

#> anova(with(ChickWeight, lm(weight ~ Time + Diet)))
#Analysis of Variance Table
#
#Response: weight
#           Df  Sum Sq Mean Sq  F value    Pr(>F)
#Time        1 2042344 2042344 1576.460 < 2.2e-16 ***
#Diet        3  129876   43292   33.417 < 2.2e-16 ***
#Residuals 573  742336    1296
#write.csv(file='ChickWeight.csv', x=ChickWeight, row.names=F)

cw = pd.read_csv('ChickWeight.csv')
cw_lm=ols('weight ~ Time + Diet', data=cw).fit()

print(sm.stats.anova_lm(cw_lm, typ=2))
#                  sum_sq   df            F         PR(>F)
#Time      2024187.608511    1  1523.368567  9.008821e-164
#Diet       108176.538530    1    81.411791   2.730843e-18
#Residual   764035.638024  575          NaN            NaN

Head and tail of the datasets are the same*, also mean, min, max, median of weight and time.

A: Looks like "Diet" only has one degree of freedom in the statsmodels call which means it was probably treated as a continuous variable whereas in R it has 3 degrees of freedom so it probably was a factor/discrete random variable.
To make ols() treat "Diet" as a categorical random variable, use
cw_lm=ols('weight ~ C(Diet) + Time', data=cw).fit()


Q: Success and failure actions when downloading a file in MVC A button on my page calls a controller method that returns a file, like so:
// Update number of downloads in database
// Return the file
return File(filedata, contentType);

In the view, this button is defined as:
<img onclick="location.href ='@Url.Action("DownloadVersion", new { fileVersionId = version.FileVersionId })'" src="@Url.Content("~/Images/download.png")", alt="Download" title="Download"/>

I need an element on my page to update dynamically with the number of downloads once the download has started. At the moment I have no way of informing the page to update. Currently I don't see a way of doing this.
I have tried calling the controller method via ajax instead so I get the success and error callbacks, however this seems to mean that the browser never actually downloads the file (I have read in other posts that this seems to be a limitation of ajax).
Unfortunately, a direct link to the file is not an option. The download has to occur via the controller method so that the user does not have access to the file url path (to check if they are authorized to download it).
Is there a viable solution to this?

A: You could do something similar to - https://stackoverflow.com/a/3667379/33116 - you can then instead of redirecting you can do a request to a web api end point which can return the download count and update an element with the returned value.


Q: NSMutableDictionary overrides the first value This is my sample code here  i calculated break time for two entries.if i add a another entries means it is overriding the previous break time ..i dont know where i am doing wrong
This is my first sample i got this for 2 entries
This is my second sample i got this for 3 entries
Here is my sample code
NSMutableArray *arrData = [[data objectForKey:@"data"]mutableCopy];

                 NSLog(@"%@",arrData);
NSLog display this data before adding 3entry ..
                     {
    date = "2016-01-20";
    "end_time" = "11:10:00";
    "function_code" = RCV;
    "operator_id" = JOHN;
    "start_time" = "11:00:00";
    "total_time" = 10;
    "total_units" = 19;
},
    {
    date = "2016-01-20";
    "end_time" = "12:25:00";
    "function_code" = PIK;
    "operator_id" = JOHN;
    "start_time" = "12:15:00";
    "total_time" = 10;
    "total_units" = 26;
})

              NSMutableDictionary *thirdEntry = [NSMutableDictionary dictionary];

                 [thirdEntry setObject:@"02:00:00" forKey:@"end_time"];
                 [thirdEntry setObject:@"PUL" forKey:@"function_code"];
                 [thirdEntry setObject:@"01:25:00" forKey:@"start_time"];
                 [thirdEntry setObject:@"45" forKey:@"total_units"];
                 [thirdEntry setObject:@"20" forKey:@"total_time"];
                 [arrData addObject:thirdEntry];

                 NSLog(@"%@",arrData);

                 //dictionary
                 NSMutableDictionary *dictData =[NSMutableDictionary dictionary];
                 NSMutableDictionary *dictData1=[NSMutableDictionary dictionary];

                 NSMutableArray *arrayProgressDate =[NSMutableArray array];

                  //storing the breaktime values

                 NSMutableDictionary *dictValues =[NSMutableDictionary dictionary];

                    for (int i =0; i<arrData.count; i++) {

                        dictData =arrData[i];

                        [arrayProgressDate addObject:dictData];

                        NSString *strendTime1 =[dictData objectForKey:@"end_time"];

                        NSLog(@"%@",strendTime1);

                            [dictValues setObject:strendTime1 forKey:@"end_time_value"];

                        if ((i + 1) < arrData.count) {

                            dictData1=arrData[i+1];

                            NSString *strStartTimeNext = [dictData1 objectForKey:@"start_time"];

                            NSLog(@"%@",strStartTimeNext
                                  );
                            //[arrayProgressDate addObject:strStartTimeNext];
                           [dictValues setObject:strStartTimeNext forKey:@"start_time_value"];

                            //calculating  break time

                        NSDate *endTimeDate = [[DateHelper sharedHelper ] dateFromString:strendTime1 withFormat:@"HH:mm:ss"];
                        NSDate *startTimeDate = [[DateHelper sharedHelper]dateFromString:strStartTimeNext withFormat:@"HH:mm:ss"];

                            NSTimeInterval timeElapsedInSeconds = [endTimeDate timeIntervalSinceDate:startTimeDate];

                        double hours = timeElapsedInSeconds / 3600.0;
                            NSLog(@"%f",hours);

                        int breakTimeInMinutes = timeElapsedInSeconds/60;
                            breakTimeInMinutes =ABS(breakTimeInMinutes);

                            NSString *newStr =[NSString stringWithFormat:@"%i",breakTimeInMinutes];

                            NSLog(@"%@",newStr);

                        [dictValues setObject:newStr forKey:@"break_time"];

                            [arrayProgressDate addObject:dictValues];

                        }

                        NSLog(@"%@",dictData);

                    }

                 NSLog(@"%@",arrayProgressDate);

Please say me what i am doing wrong in this , i want to calculate break time.

A: This line NSMutableDictionary *dictValues =[NSMutableDictionary dictionary]; should be inside for loop.
While finding the break time you must consider the date as well. Otherwise you will get wrong values.
NSMutableDictionary *thirdEntry = [NSMutableDictionary dictionary];

[thirdEntry setObject:@"02:00:00" forKey:@"end_time"];
[thirdEntry setObject:@"PUL" forKey:@"function_code"];
[thirdEntry setObject:@"01:25:00" forKey:@"start_time"];
[thirdEntry setObject:@"45" forKey:@"total_units"];
[thirdEntry setObject:@"20" forKey:@"total_time"];
[arrData addObject:thirdEntry];

NSLog(@"%@",arrData);

//dictionary
NSMutableDictionary *dictData =[NSMutableDictionary dictionary];
NSMutableDictionary *dictData1=[NSMutableDictionary dictionary];

NSMutableArray *arrayProgressDate =[NSMutableArray array];

for (int i =0; i<arrData.count; i++) {

    dictData =arrData[i];

    [arrayProgressDate addObject:dictData];

    NSString *strendTime1 =[dictData objectForKey:@"end_time"];

    NSLog(@"%@",strendTime1);

    //storing the breaktime values

    NSMutableDictionary *dictValues =[NSMutableDictionary dictionary];
    [dictValues setObject:strendTime1 forKey:@"end_time_value"];

    if ((i + 1) < arrData.count) {

        dictData1=arrData[i+1];

        NSString *strStartTimeNext = [dictData1 objectForKey:@"start_time"];

        NSLog(@"%@",strStartTimeNext
              );
        //[arrayProgressDate addObject:strStartTimeNext];
        [dictValues setObject:strStartTimeNext forKey:@"start_time_value"];

        //calculating  break time

        NSDate *endTimeDate = [[DateHelper sharedHelper ] dateFromString:strendTime1 withFormat:@"HH:mm:ss"];
       NSDate *startTimeDate = [[DateHelper sharedHelper]dateFromString:strStartTimeNext withFormat:@"HH:mm:ss"];

        NSTimeInterval timeElapsedInSeconds = [endTimeDate timeIntervalSinceDate:startTimeDate];

        double hours = timeElapsedInSeconds / 3600.0;
        NSLog(@"%f",hours);

        int breakTimeInMinutes = timeElapsedInSeconds/60;
        breakTimeInMinutes =ABS(breakTimeInMinutes);

        NSString *newStr =[NSString stringWithFormat:@"%i",breakTimeInMinutes];

        NSLog(@"%@",newStr);

        [dictValues setObject:newStr forKey:@"break_time"];

        [arrayProgressDate addObject:dictValues];

    }

    NSLog(@"%@",dictData);

}
    NSLog(@"%@",arrayProgressDate);


Q: Converting Invalid DateTime I need to Convert DateTime from UTC to local time, For that I had Validated the date time before converting to local time using TimeZoneInfo IsInValidTime method.
I'm getting Invalid date time for a particular date time, How to convert this date to a valid one?
Here is the Sample code:
_timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var dateTime = "10/03/2013 2:12:00 AM";
DateTime universalFormatDateTime = Convert.ToDateTime(dateTime).GetUniversalFormatDateTime();
if (_timeZoneInfo.IsInvalidTime(universalFormatDateTime)) Console.Write("Invalid DateTime\n");

A: What framework are you using?
Isn't ToUniversalTime() the correct choice?
DateTime universalFormatDateTime = Convert.ToDateTime(dateTime).ToUniversalTime()

A: You should specify the DateTimeKind of your Date time. Add this before perform the validation:
universalFormatDateTime = DateTime
   .SpecifyKind(universalFormatDateTime,DateTimeKind.Local);

A: I guess this is what you're trying to achieve:
_timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var dateTime = "10/03/2013 2:12:00 AM";
DateTime universalFormatDateTime = Convert
                                  .ToDateTime(dateTime, new CultureInfo("en-GB"))
                                  .ToUniversalTime();
if (_timeZoneInfo.IsInvalidTime(universalFormatDateTime))
    Console.WriteLine("Invalid DateTime");
else
    Console.WriteLine("Valid DateTime");

You can look at the Convert.ToDateTime articke for future reference.


Q: How to verify multiple CSS attributes in one method I'm a newbie setting up a python selenium framework and am wondering if I should collect and verify CSS attributes more tidily?
I currently have the verify css function set up in the base page and these methods which define the individual css attributes in the code below (see image_thumb & image_height):
class BrokenImagePage(BasePage):
    _broken_image =  {"by": By.XPATH, "value": "//img[1]"}
    _placeholder_image = {"by": By.XPATH, "value": "//img[3]"}
    _title = {"by" : By.XPATH, "value": "//div[@class='example']/h3"}

    def __init__(self, driver):
        self.driver = driver
        self._visit("/broken_images")
        assert self._is_displayed(self._title)

    def image_present (self):
        return self._is_displayed(self._broken_image)

    def image_thumb (self):
        return self._wait_for_is_displayed(self._placeholder_image, 5)

    def image_height (self):
        return self._verify_css_value(self._placeholder_image, 'height')

    def image_width (self):
        return self._verify_css_value(self._placeholder_image, 'width')

Then I assert against the attribute in my test like so:
 def test_image_css(self, images):
    assert images.image_height() == '90px'
    assert images.image_width() == '120px'

This doesn't seem very efficient to me. Could I collect all the attributes in one method and assert against those in a better way?


Q: yum install failed and corrupted the original yum I was trying to install yum-3.4.3 using commands make && make install, but it failed with showing the following log:
infra-bld4:/tmp/hxu2/yum-3.4.3> make
for d in rpmUtils yum etc docs po; do make PYTHON=python -C $d; [ $? = 0 ] || exit 1 ; done
make-3.79.1-p7[1]: Entering directory `/tmp/hxu2/yum-3.4.3/rpmUtils'
echo "Nothing to do"
Nothing to do
make-3.79.1-p7[1]: Leaving directory `/tmp/hxu2/yum-3.4.3/rpmUtils'
make-3.79.1-p7[1]: Entering directory `/tmp/hxu2/yum-3.4.3/yum'
echo "Nothing to do"
Nothing to do
make-3.79.1-p7[1]: Leaving directory `/tmp/hxu2/yum-3.4.3/yum'
make-3.79.1-p7[1]: Entering directory `/tmp/hxu2/yum-3.4.3/etc'
echo "Nothing to do"
Nothing to do
make-3.79.1-p7[1]: Leaving directory `/tmp/hxu2/yum-3.4.3/etc'
make-3.79.1-p7[1]: Entering directory `/tmp/hxu2/yum-3.4.3/docs'
echo "Nothing to do"
Nothing to do
make-3.79.1-p7[1]: Leaving directory `/tmp/hxu2/yum-3.4.3/docs'
make-3.79.1-p7[1]: Entering directory `/tmp/hxu2/yum-3.4.3/po'
msgfmt -o ca.mo ca.po -c
msgfmt: ca.po: field `Language-Team' still has initial default value
msgfmt: found 1 fatal error
make-3.79.1-p7[1]: *** [ca.mo] Error 1
make-3.79.1-p7[1]: Leaving directory `/tmp/hxu2/yum-3.4.3/po'
make-3.79.1-p7: *** [subdirs] Error 1
infra-bld4:/tmp/hxu2/yum-3.4.3>

Because I tried to re-install the yum, but the installation failed half way, so bad thing is that I corrupted the original yum in the system. Anybody can help me recover the yum or re-install it will be highly appreciated.
Thanks!

A: if you want to uninstall
you can do rpm -e yum
then
Install it using:
rpm -ivh yum-(version).rpm
If yum is working fine for local installations, but it's not able to access Red Hat Network, verify if the following packages are installed. If not, install them:
rhnsd
yum-rhn-plugin
yum-security
rhn-check
rhn-setup
rhn-setup-gnome
yum-downloadonly
rhn-client-tools
rhn-virtualization-common
rhn-virtualization-host
pirut
yum-updatesd


Q: How do i speed up a python nested loop? I'm trying to calculate the gravity effect of a buried object by calculating the effect on each side of the body then summing up the contributions to get one measurement at one station, an repeating for a number of stations. the code is as follows( the body is a square and the code calculates clockwise around it, that's why it goes from -x back to -x coordinates)
grav = []
x=si.arange(-30.0,30.0,0.5)

#-9.79742526     9.78716693    22.32153704    27.07382349  2138.27146193
xcorn = (-9.79742526,9.78716693 ,9.78716693 ,-9.79742526,-9.79742526)
zcorn = (22.32153704,22.32153704,27.07382349,27.07382349,22.32153704)
gamma = (6.672*(10**-11))#'N m^2 / Kg^2'
rho = 2138.27146193#'Kg / m^3'
grav = []
iter_time=[]
def procedure():
    for i in si.arange(len(x)):# cycles position
        t0=time.clock()
        sum_lines = 0.0

        for n in si.arange(len(xcorn)-1):#cycles corners
            x1 = xcorn[n]-x[i]
            x2 = xcorn[n+1]-x[i]
            z1 = zcorn[n]-0.0  #just depth to corner since all observations are on the surface.
            z2 = zcorn[n+1]-0.0
            r1 = ((z1**2) + (x1**2))**0.5
            r2 = ((z2**2) + (x2**2))**0.5
            O1 = si.arctan2(z1,x1)
            O2 = si.arctan2(z2,x2)
            denom = z2-z1
            if denom == 0.0:
                denom = 1.0e-6

            alpha = (x2-x1)/denom

            beta = ((x1*z2)-(x2*z1))/denom
            factor = (beta/(1.0+(alpha**2)))
            term1 = si.log(r2/r1)#log base 10
            term2 = alpha*(O2-O1)
            sum_lines = sum_lines + (factor*(term1-term2))
        sum_lines = sum_lines*2*gamma*rho
        grav.append(sum_lines)
        t1 = time.clock()
        dt = t1-t0
        iter_time.append(dt)

Any help in speeding this loop up would be appreciated Thanks.

A: Your xcorn and zcorn values repeat, so consider caching the result of some of the computations.
Take a look at the timeit and profile modules to get more information about what is taking the most computational time.

A: It is very inefficient to access individual elements of a numpy array in a Python loop. For example, this Python loop:
for i in xrange(0, len(a), 2):
    a[i] = i

would be much slower than:
a[::2] = np.arange(0, len(a), 2)

You could use a better algorithm (less time complexity) or use vector operations on numpy arrays as in the example above. But the quicker way might be just to compile the code using Cython:
#cython: boundscheck=False, wraparound=False
#procedure_module.pyx
import numpy as np
cimport numpy as np

ctypedef np.float64_t dtype_t

def procedure(np.ndarray[dtype_t,ndim=1] x,
              np.ndarray[dtype_t,ndim=1] xcorn):
    cdef:
        Py_ssize_t i, j
        dtype_t x1, x2, z1, z2, r1, r2, O1, O2
        np.ndarray[dtype_t,ndim=1] grav = np.empty_like(x)
    for i in range(x.shape[0]):
        for j in range(xcorn.shape[0]-1):
            x1 = xcorn[j]-x[i]
            x2 = xcorn[j+1]-x[i]
            ...
        grav[i] = ...
    return grav

It is not necessary to define all types but if you need a significant speed up compared to Python you should define at least types of arrays and loop indexes.
You could use cProfile (Cython supports it) instead of manual calls to time.clock().
To call procedure():
#!/usr/bin/env python
import pyximport; pyximport.install() # pip install cython
import numpy as np

from procedure_module import procedure

x = np.arange(-30.0,30.0,0.5)
xcorn = np.array((-9.79742526,9.78716693 ,9.78716693 ,-9.79742526,-9.79742526))
grav = procedure(x, xcorn)


Q: Release unused memory sql server 2014 memory optimized table My system

Microsoft SQL Server 2014 (SP1-CU4) (KB3106660) - 12.0.4436.0 (X64)
  Dec  2 2015 16:09:44  Copyright (c) Microsoft Corporation Enterprise
  Edition: Core-based Licensing (64-bit) on Windows NT 6.3  (Build
  9600: ) (Hypervisor)

I use two table table1 and ``table2` memory optimazed table (each size 27 GB)
drop table1
IF OBJECT_ID('table1') IS NOT NULL

BEGIN

    DROP TABLE [dbo].[table1]

END

After:
SQL server Memory Usage By Memory Optimazed Objects Reports
Table Name =table2  Table Used Memory = 26582,50  Table Unused Memory = 26792,69

How can I run sql server garbage collector manually ? this is possible or not ?
I need "Table Unused Memory" Release because another process always gives this error

"There is insufficient system memory in resource pool 'Pool' to run this query."

Thank you

A: Data for memory optimized tables is held in data & delta files.
A delete statement will not remove the data from the data file but insert a delete record into the delta file, hence your storage continuing to be large.
The data & delta file are maintained in pairs know as checkpoint file pairs (CFPs). Over time closed CFPs are merged based upon a merge policy from multiple CFPs into one merged target CFP.
A background thread evaluates all closed CFPs using a merge policy and then initiates one or more merge requests for the qualifying CFPs. These merge requests are processed by the offline checkpoint thread. The evaluation of merge policy is done periodically and also when a checkpoint is closed.
You can force merge the files using stored procedure sys.sp_xtp_merge_checkpoint_files following a checkpoint.

EDIT
Run statement:
SELECT
    container_id,
    internal_storage_slot,
    file_type_desc,
    state_desc,
    inserted_row_count,
    deleted_row_count,
    lower_bound_tsn,
    upper_bound_tsn
FROM
    sys.dm_db_xtp_checkpoint_files
ORDER BY
    file_type_desc,
    state_desc

Then find the rows with status UNDER CONSTRUCTION and make a note of
  the lower and upper transaction id.
Now execute:
EXEC sys.sp_xtp_merge_checkpoint_files 'myDB',1003,1004;

where 1003 and 1004 is the lower and upper transaction id.

To completely remove the files you will have to ensure that you have to:

*

*Run Select statement from above

*Run EXEC sys.sp_xtp_merge_checkpoint_files from above

*Perform a Full Backup

*CHECKPOINT

*Backup the Log

*EXEC sp_xtp_checkpoint_force_garbage_collection;

*Checkpoint

*Exec sp_filestream_force_garbage_collection 'MyDb' to remove files marked as Tombstone

You may need to run steps 3 - 7 twice to completely get rid of the files.
See The DBA who came to tea article
CFP's go through the following stages:
•PRECREATED – A small set of CFPs are kept pre-allocated to minimize or eliminate any waits to allocate new files as transactions are being executed. These are full sized with data file size of 128MB and delta file size of 8 MB but contain no data. The number of CFPs is computed as the number of logical processors or schedulers with a minimum  of 8. This is a fixed storage overhead in databases with memory-optimized tables
•UNDER CONSTRUCTION – Set of CFPs that store newly inserted and possibly deleted data rows since the last checkpoint.
•ACTIVE - These contain the inserted/deleted rows from previous closed checkpoints. These CFPs contain all required inserted/deleted rows required before applying the active part of the transaction log at the database restart. We expect that size of these CFPs to be approximately 2x of the in-memory size of memory-optimized tables assuming merge operation is keeping up with the transactional workload.
•MERGE TARGET – CFP stores the consolidated data rows from the CFP(s) that were identified by the merge policy. Once the merge is installed, the MERGE TARGET transitions into ACTIVE state
•MERGED SOURCE – Once the merge operation is installed, the source CFPs are marked as MERGED SOURCE. Note, the merge policy evaluator may identify multiple merges a CFP can only participate in one merge operation.
•REQUIRED FOR BACKUP/HA – Once the merge has been installed and the MERGE TARGET CFP is part of durable checkpoint, the merge source CFPs transition into this state. CFPs in this state are needed for operational correctness of the database with memory-optimized table.  For example, to recover from a durable checkpoint to go back in time.  A CFP can be marked for garbage collection once the log truncation point moves beyond its transaction range.
•IN TRANSITION TO TOMBSTONE – These CFPs are not needed by in-memory OLTP engine can they can be garbage collected. This state indicates that these CFPs are waiting for the background thread to transition them to the next state TOMBSTONE
•TOMBSTONE – These CFPs are waiting to be garbage collected by the filestream garbage collector.  Please refer to FS Garbage Collection for details


Q: OpenGL cube coloring I created a cube using OpenGL2.0. I want it has six different color on its each face. I followed some example and draw one cube. Everything goes well except the color of the cube. Color of each face mixed together and only red and green are shown. It looks really wired and I did't see any people have this problem same as me. Can anybody give me a hand? Below are my code and cube. Thank you so much!
enter image description here
public class MyCube {
private FloatBuffer vertexBuffer;
private ShortBuffer drawListBuffer;
private ShortBuffer[] ArrayDrawListBuffer;
private FloatBuffer colorBuffer;

private int mProgram;

//For Projection and Camera Transformations
private final String vertexShaderCode =
        // This matrix member variable provides a hook to manipulate
        // the coordinates of the objects that use this vertex shader
        "uniform mat4 uMVPMatrix;" +
                "attribute vec4 vPosition;" +
                "attribute vec4 vColor;" +
                "varying vec4 vColorVarying;" +
                "void main() {" +
                // the matrix must be included as a modifier of gl_Position
                // Note that the uMVPMatrix factor *must be first* in order
                // for the matrix multiplication product to be correct.
                "  gl_Position = uMVPMatrix * vPosition;" +
                "vColorVarying = vColor;"+
                "}";

// Use to access and set the view transformation
private int mMVPMatrixHandle;

private final String fragmentShaderCode =
        "precision mediump float;" +
                "varying vec4 vColorVarying;"+
                "void main() {" +
                "  gl_FragColor = vColorVarying;" +
                "}";

// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
float cubeCoords[] = {
        -0.5f, 0.5f, 0.5f,   // front top left 0
        -0.5f, -0.5f, 0.5f,   // front bottom left 1
        0.5f, -0.5f, 0.5f,   // front bottom right 2
        0.5f, 0.5f, 0.5f,  // front top right 3
        -0.5f, 0.5f, -0.5f,   // back top left 4
        0.5f, 0.5f, -0.5f,   // back top right 5
        -0.5f, -0.5f, -0.5f,   // back bottom left 6
        0.5f, -0.5f, -0.5f,  // back bottom right 7
        };

// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };
float red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
float blue[] = { 0.0f, 0.0f, 1.0f, 1.0f };

private short drawOrder[] = {
                                0, 1, 2, 0, 2, 3,//front
                                0, 4, 5, 0, 5, 3, //Top
                                0, 1, 6, 0, 6, 4, //left
                                3, 2, 7, 3, 7 ,5, //right
                                1, 2, 7, 1, 7, 6, //bottom
                                4, 6, 7, 4, 7, 5 //back
                                }; //(order to draw vertices)

final float cubeColor[] =
        {
                // Front face (red)
                        1.0f, 0.0f, 0.0f, 1.0f,
                        1.0f, 0.0f, 0.0f, 1.0f,
                        1.0f, 0.0f, 0.0f, 1.0f,
                        1.0f, 0.0f, 0.0f, 1.0f,
                        1.0f, 0.0f, 0.0f, 1.0f,
                        1.0f, 0.0f, 0.0f, 1.0f,

                // Top face (green)
                        0.0f, 1.0f, 0.0f, 1.0f,
                        0.0f, 1.0f, 0.0f, 1.0f,
                        0.0f, 1.0f, 0.0f, 1.0f,
                        0.0f, 1.0f, 0.0f, 1.0f,
                        0.0f, 1.0f, 0.0f, 1.0f,
                        0.0f, 1.0f, 0.0f, 1.0f,

                // Left face (blue)
                       0.0f, 0.0f, 1.0f, 1.0f,
                        0.0f, 0.0f, 1.0f, 1.0f,
                        0.0f, 0.0f, 1.0f, 1.0f,
                        0.0f, 0.0f, 1.0f, 1.0f,
                        0.0f, 0.0f, 1.0f, 1.0f,
                        0.0f, 0.0f, 1.0f, 1.0f,

                // Right face (yellow)
                       1.0f, 1.0f, 0.0f, 1.0f,
                        1.0f, 1.0f, 0.0f, 1.0f,
                        1.0f, 1.0f, 0.0f, 1.0f,
                        1.0f, 1.0f, 0.0f, 1.0f,
                        1.0f, 1.0f, 0.0f, 1.0f,
                        1.0f, 1.0f, 0.0f, 1.0f,

                // Bottom face (cyan)
                       0.0f, 1.0f, 1.0f, 1.0f,
                        0.0f, 1.0f, 1.0f, 1.0f,
                        0.0f, 1.0f, 1.0f, 1.0f,
                        0.0f, 1.0f, 1.0f, 1.0f,
                        0.0f, 1.0f, 1.0f, 1.0f,
                        0.0f, 1.0f, 1.0f, 1.0f,

                // Back face (magenta)
                       1.0f, 0.0f, 1.0f, 1.0f,
                        1.0f, 0.0f, 1.0f, 1.0f,
                        1.0f, 0.0f, 1.0f, 1.0f,
                        1.0f, 0.0f, 1.0f, 1.0f,
                        1.0f, 0.0f, 1.0f, 1.0f,
                        1.0f, 0.0f, 1.0f, 1.0f
        };

public MyCube() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            cubeCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(cubeCoords);
    vertexBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    // initialize byte buffer for the color list
    ByteBuffer cb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            cubeColor.length * 4);
    cb.order(ByteOrder.nativeOrder());
    colorBuffer = cb.asFloatBuffer();
    colorBuffer.put(cubeColor);
    colorBuffer.position(0);

    int vertexShader = MyRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
            vertexShaderCode);
    int fragmentShader = MyRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
            fragmentShaderCode);

    // create empty OpenGL ES Program
    mProgram = GLES20.glCreateProgram();

    // add the vertex shader to program
    GLES20.glAttachShader(mProgram, vertexShader);

    // add the fragment shader to program
    GLES20.glAttachShader(mProgram, fragmentShader);

    // creates OpenGL ES program executables
    GLES20.glLinkProgram(mProgram);
}

private int mPositionHandle;
private int mColorHandle;

private final int vertexCount = cubeCoords.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex

public void draw(float[] mvpMatrix) { // pass in the calculated transformation matrix
    // Add program to OpenGL ES environment
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    // get handle to fragment shader's vColor member
    mColorHandle = GLES20.glGetAttribLocation(mProgram, "vColor");

    // Enable a handle to the cube vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    // Prepare the cube coordinate data
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
            GLES20.GL_FLOAT, false,
            vertexStride, vertexBuffer);

    // Set color for drawing the triangle
    //mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
    // Enable a handle to the cube colors
    GLES20.glEnableVertexAttribArray(mColorHandle);
    // Prepare the cube color data
    GLES20.glVertexAttribPointer(mColorHandle, 4, GLES20.GL_FLOAT, false, 16, colorBuffer);

    // get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

    // Pass the projection and view transformation to the shader
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

    // Draw the cube
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
    //GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
    GLES20.glDisableVertexAttribArray(mColorHandle);
    GLES20.glDisableVertexAttribArray(mMVPMatrixHandle);
}

}

A: The index buffer is used to index into the colorBuffer using the same index as is used for indexing into the vertexBuffer, so the corresponding elements in each need to match. The indices in your index buffer are in the range of 0-7, so you will only ever index the first 8 entries of your colorBuffer, which are green and red.
You need to have a separate index for every unique combination of vertex position and color. For each face there are 4 unique vertex-color combinations, so you will need 6 * 4 = 24 entries in your cubeCoords array and 24 matching entries in your cubeColor array.
Like this:
float cubeCoords[] = {

    // front face
    -0.5f, 0.5f, 0.5f,   // front top left 0
    -0.5f, -0.5f, 0.5f,   // front bottom left 1
    0.5f, -0.5f, 0.5f,   // front bottom right 2
    0.5f, 0.5f, 0.5f,  // front top right 3

    // top face
    -0.5f, 0.5f, -0.5f,   // back top left 4
    -0.5f, 0.5f, 0.5f,   // front top left 5
    0.5f, 0.5f, 0.5f,  // front top right 6
    0.5f, 0.5f, -0.5f,   // back top right 7

    // other faces...
}

final float cubeColor[] =
{
        // Front face (red)
                1.0f, 0.0f, 0.0f, 1.0f,
                1.0f, 0.0f, 0.0f, 1.0f,
                1.0f, 0.0f, 0.0f, 1.0f,
                1.0f, 0.0f, 0.0f, 1.0f,

        // Top face (green)
                0.0f, 1.0f, 0.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,
                0.0f, 1.0f, 0.0f, 1.0f,

            // other faces...
}

private short drawOrder[] = {
                            0, 1, 2, 0, 2, 3,//front
                            4, 5, 6, 4, 6, 7, //Top
                            // other faces...
}


Q: How to call GET/POST method programatically using spring I want to call GET/POST method programatically from within the java class using Spring. I have done this stuff in Servlet class before but I am not clear how to do this with spring. I went through some related tutorials but I am still not cleared. Can any one please explain how to do this?
Thanks.

A: Since you are working on a Spring based application, I would suggest using Spring RestTemplate to request your GET/POST endpoints.
The following may be a short snippet of what could be done and you can refer to this Spring tutorials (1,2 and 3) for more details:
public void getOrPostTest() {

  String GET_URL = "http://localhost:8080/somepath";

  RestTemplate restTemplate = new RestTemplate();

  Map<String, String> params = new HashMap<String, String>();
  params.put("prop1", "1");
  params.put("prop2", "value");

  String result = restTemplate.getForObject(GET_URL, String.class, params);
}

A: You can use HttpClient, look this example.
      HttpClient httpClient = login(HTTP_SERVER_DOMAIN, "user1@gmail.com", "password");
    GetMethod getAllAdvicesMethod = new GetMethod(URL);
    getAllAdvicesMethod
            .addRequestHeader("Content-Type", "application/json");
    try {
        httpClient.executeMethod(getAllAdvicesMethod);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

If you need another method request you can changes GetMethod for
        PostMethod postDateMethod = new PostMethod(URL);


Q: zabbix monitor JMX C3P0 tokenid the key is:
jmx["com.mchange.v2.c3p0:identityToken=2yaf3o9m1taosztt7mari|2294069,name=2yaf3o9m1taosztt7mari|2294069,type=PooledDataSource",maxPoolSize]

but the identityToken changed when restart the tomcat.
is there a macro to define it,adapt when changed?
enter image description here

A: You can configure c3p0's JMX key to be something that will not change.
Please see http://www.mchange.com/projects/c3p0/#jmx_configuration_and_management
The simple story is:

*

*Be sure to set the c3p0 configuration property dataSourceName, which will become the value of a name attribute in the JMX key;

*Set (in a c3p0.properties file or as a system property or in typesafe-config file) com.mchange.v2.c3p0.management.ExcludeIdentityToken=true
If you are using a c3p0.properties file, it'd be something like
c3p0.dataSourceName=myPooledDataSource
com.mchange.v2.c3p0.management.ExcludeIdentityToken=true


Q: Copying a string to a struct member I can't figure out how to copy a string from inputString to newNode->data.
My struct looks like this:
typedef struct node {
    char *data;
    struct node *left;
    struct node *right;
} node;

And the function in questions looks like this:
node* addToTree(char inputString[]) {
    node *newNode;

    if ((newNode = malloc(sizeof(node*))) == NULL) {
        printf("Error: could not allocate memory");
        exit(-1);
    }

    if ((newNode->data = malloc(strlen(inputString) + 1)) == NULL) {
        printf("Error: could not allocate memory");
        exit(-1);
    }

    /* This line of code doesn't seem to copy anything to newNode->data.
       This is the way I believe should work, however I don't understand what the
       problem with it is. I have tried strlcpy and strncpy as well. */
    strcpy(newNode->data, inputString);

    /* The line below here seems to work when I print the value
       within the function, but some of the values are garbage when
       I try to use them later on in the program. */
    newNode->data = inputString;

    newNode->left = NULL;
    newNode->right = NULL;
    printf("Input string: %s\n", inputString);
    printf("New node data: %s\n", newNode->data);

    return newNode;
}

A: Your sizeof(node*) does not represent the size you need.
newnode = malloc(sizeof(node*))    // wrong
newnode = malloc(sizeof (node))    // correct
newnode = malloc(sizeof *newNode)  // better

Why is sizeof *newNode better?
Because it prevents accidental forgetting to update the code in two places if the type changes
struct node {
    char *data;
    struct node *next;
    struct node *prev;
};
struct nodeEx {
    char *data;
    size_t len;
    struct nodeEx *next;
    struct nodeEx *prev;
};

struct nodeEx *newnode = malloc(sizeof (struct node)); // wrong
struct nodeEx *newnode = malloc(sizeof *newnode);      // correct

A: The below line does not allocate the required amount of memory, it allocates memory equal to the size of a pointer to node.
if ((newNode = malloc(sizeof(node*))) == NULL)

So your strcpy fails because there is no memory to copy into.
Change the above to:
if ((newNode = malloc(sizeof(node))) == NULL)

What happens after you do the following is undefined behavior because the memory representing inputString can be overwritten, and that is why you get garbage values later on.
newNode->data = inputString;

You can see the top answer to this question for additional information.

A: newNode->data = inputString;

is incorrect, it overrides the previously malloced memory.
if ((newNode->data = malloc(strlen(inputString) + 1)) == NULL) {
    printf("Error: could not allocate memory");
    exit(-1);
}
strcpy(newNode->data, inputString);

is enough to allocate memory and copy the string into it.


Q: Excel VBA Data organization with splitting and duplication In Microsoft Excel, I am looking to use VBA code to sort through large amounts of data that is not optimally organized
Currently, I have data that looks like this:
Poor Data Organization
However, I would like to process the data to this:
Correct Data Organization
I have been unable to find an example for my specific situation and appreciate your time and responses.

A: This will process the data into the format you're after.  The source data is in Sheet1 and the reformatted data is placed into Sheet2:
Option Explicit

Sub MapCell2Cells()
  Dim sht1 As Worksheet, sht2 As Worksheet
  Set sht1 = Worksheets("Sheet1")
  Set sht2 = Worksheets("Sheet2")

  Dim strG As String, strL As String
  Dim arrH() As String, arrI() As String, arrJ() As String

  Dim i As Integer, j As Integer, idx As Integer, lastRow As Integer
  lastRow = sht1.Cells(Rows.Count, "G").End(xlUp).Row

  idx = 1
  For i = 1 To lastRow:

    With sht1
      strG = Replace(.Cells(i, "G").Value, vbLf, "")
      arrH = Split(.Cells(i, "H").Value, vbLf)
      arrI = Split(.Cells(i, "I").Value, vbLf)
      arrJ = Split(.Cells(i, "J").Value, vbLf)
      strL = Replace(.Cells(i, "L").Value, vbLf, "")
    End With

    With sht2
      For j = LBound(arrH) To UBound(arrH)
        .Cells(idx, "G").Value = strG
        .Cells(idx, "H").Value = arrH(j)
        .Cells(idx, "I").Value = arrI(j)
        .Cells(idx, "J").Value = arrJ(j)
        .Cells(idx, "L").Value = strL
        idx = idx + 1
      Next
    End With

  Next

End Sub


Q: Python dictionary modification This is a python code
def build_person(first_name, last_name, age=None):
Return a dictionary of information about a person.
   person = {'first': first_name, 'last': last_name}
   if age:
      person['age'] = age
   return person

I understand everything but the line
person['age'] = age

Because, "person" is a dictionary, and so if I want to modify it, shouldn't it accept a key-value pair? How can I modify it correctly ?

A: person['age'] = age

The 'age' inside the brackets is the key, the value 'age' is where your value is assigned.
The person dictionnary becomes:
 {'first': first_name, 'last': last_name,'age': age}

A: Here, person[age] = age
works only when age is given as argument when calling this function.
person is dictionary, age in person[age] is key, and the age which is at right side of assignment operator(=) is value passed as argument in function.
for e.g :
for the given code below in last line i have given age as argument.
def build_person(first_name, last_name, age=None):

   person = {'first': first_name, 'last': last_name}
   if age:
      person['age'] = age
   print(person)
   return person
build_person("yash","verma",9)

Output for above code is :
{'first': 'yash', 'last': 'verma', 'age': 9}

now,
if i don't give age as a argument then,
def build_person(first_name, last_name, age=None):

    person = {'first': first_name, 'last': last_name}
    if age:
        person['age'] = age
    print(person)
    return person
build_person("yash","verma")

output will be:
{'first': 'yash', 'last': 'verma'}


Q: Enum 2-ways binding with a Combobox I am trying to do a simple 2-ways binding with an enum to a Combobox but haven't found anything that works with my code so far.
My enum (C#):
public enum CurrenciesEnum { USD, JPY, HKD, EUR, AUD, NZD };

The property the Enum should set / is bound to:
    private string _ccy;
    public string Ccy
    {
        get
        {
            return this._ccy;
        }
        set
        {
            if (value != this._ccy)
            {
                this._ccy= value;
                NotifyPropertyChanged("Ccy");
            }
        }
    }

The Xaml code that doesn't work:
    <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
        <ObjectDataProvider x:Key="Currencies" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="ConfigManager:CurrenciesEnum" />
                </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </ResourceDictionary>
</UserControl.Resources>

<ComboBox ItemsSource="{Binding Source={StaticResource Currencies}}" SelectedItem="{Binding Ccy, Mode=TwoWay}"/>

Thank you in advance for your help!

A: Well the problem is you are binding a Enum to a string, this will only work one way due to the default ToString operation in the binding engine.
If you are only using the string value change your ObjectDataProvider method name to GetNames this will return the string values for your Enum and will bind both ways, the other option is to not bind to a string but the Enum type.
    <ObjectDataProvider x:Key="Currencies" MethodName="GetNames" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="ConfigManager:CurrenciesEnum" />
            </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

A: I load the enum into a Dictionary
public static Dictionary<T, string> EnumToDictionary<T>()
        where T : struct
{
    Type enumType = typeof(T);

    // Can't use generic type constraints on value types,
    // so have to do check like this
    if (enumType.BaseType != typeof(Enum))
        throw new ArgumentException("T must be of type System.Enum");

    Dictionary<T, string> enumDL = new Dictionary<T, string>();
    //foreach (byte i in Enum.GetValues(enumType))
    //{
    //    enumDL.Add((T)Enum.ToObject(enumType, i), Enum.GetName(enumType, i));
    //}
    foreach (T val in Enum.GetValues(enumType))
    {
        enumDL.Add(val, val.ToString());
    }
    return enumDL;
}


Q: WCF with Flash tutorial I'm a beginner in WCF, which I have chosen instead of Web Services because all articles and blogs I've read seem to point out that ASMX is old news.
I have read a bit about the differences between old Web Services and WCF, and I got the general idea. I also took the MSDN WCF tutorial which seemed simple enough.
My problem is that I want to create WCF services that can be consumed by Flash. I've read that it's doable everywhere, but with no obvious A-Z tutorial on how to proceed with the server-side and client-side... Just some suggestions.
Can anyone point me to the right direction, with a brief explanation of the options available in front of me?

A: We do this with our games where we have a bunch of WCF services provide different functionalities to the Flash clients running in Facebook/MySpace, etc.
I suggest you should first have a look at this codeplex project:
http://wcfflashremoting.codeplex.com/
It allows you to implement a AMF endpoint for communicating with the Flash clients.
All your DataContract need to be mapped exactly including namespace and property names on both sides, so if you have a MyProject.Contracts.Requests.HandShakeRequest object in your WCF project the Flash client needs to have a replicate defined in the SAME namespace.
Another which we find very helpful is the request/response pattern because it allows to add/remove parameter/output values easily and have a fair amount of backward compatibility - add a new parameter to the Request object on the server for a new feature and the client doesn't HAVE TO send the new parameter right away.
For debugging you absoluately need Charles (http://www.charlesproxy.com), the latest version should have the AMF viewer working properly (I think you used to have to download an add-in) so you can see the AMF messages coming back from the server in a nice, readable format.
Hope this helps! There are some other caveats around working with a Flash client from WCF but can't remember them off the top of my head :-P so have a play around with that remoting extension and I'll pop some other bits and bobs down when I can remember them!


Q: POST variable to another class I have a search function using XPath, when results are shown a check box is also echoed.
<form name="save" method="POST" action="saveProcess.php">
            <?php
            foreach ($holidays as $holiday)
            {
                $resultTable .=  "
link}\">{$holiday->title}" . "
" .
                "{$holiday->pubDate}" . "
" .
                "{$holiday->description}" . "<input type='checkbox' name='chk' value='{$holiday->title}' />" . "

";
            }
            ?>
            <input type="submit" value="submit"/>
        </form>

I would like this check box to hold the value of {$holiday->title} which when the form is submitted will be shown in saveProcess.php, i use the isset method to check if the variable is set and it is not.
if (isset($_POST['chk'])) {
    echo $_POST['chk'];
}
else
{
    echo"variable is not set";
}

Where am i going wrong?

A: Your code looks ok to me, just remember that the value of a checkbox is posted only if the checkbox is checked, if it's not checket $_POST['chk'] is not set
EDIT - since you are revriting your checkboxes as suggested in the comment use an array
        <?php
        foreach ($holidays as $holiday)
        {
            $resultTable .=  "
link}\">{$holiday->title}" . "
" .
            "{$holiday->pubDate}" . "
" .
            "{$holiday->description}" . "<input type='checkbox' name='chk[]' value='{$holiday->title}' />" . "

";
        }
        ?>

And then server side $_POST['chk'] will be ann array

A: The problem is that you name each checkbox "chk", and when you submit the form, the values get overwritten. That's why it doesn't get anything in saveProcess.php. What you need to do, is either specify that the $_POST["chk"] can contain an array of value, like so:
<input type='checkbox' name='chk[]' value='{$holiday->title}' />

Notice the square brackets in the name. Now $_POST["chk"] will be an array.
Another way, would be to leave the html as it is, and just get the data, in saveProcess.php, using:
$HTTP_POST_VARS["chk"]

The first part basically explains why it doesn't work and how to fix it, while the second suggestion, is merely an alternate way of getting the data.
Have a great day!


Q: What is the difference between Pointer and strings? What is the difference between a pointer and array or they are same? As a array also works with poiter arithematic so it can be said that an array is nothing but pointer to its fitst element.

A: They both are different by the following differences:-
int array[40];
int * arrayp;

Now if you will try to see the size of both then it will be different for pointer it will same everytime whereas for array it varies with your array size
sizeof(array);\\Output 80
sizeof(arrayp);\\Output 4(on 32-bit machines)

Which means that computer treats all the offsprings of integers in an array as one which could not be possible with pointers.
Secondly, perform increment operation.
array++;\\Error
arrayp++;\\No error

If an array could have been a pointer then that pointer's pointing location could have been changes as in the second case with arrayp but it is not so.


Q: java.lang.OutOfMemoryError: GC overhead limit exceeded in PYSPARK My scenario of spark job is to connect to PostgreSQL database, read the data from PSQL and performing aggregations after reading the data. In this process I am able to establish database connection successfully and while selecting rows from a table I am facing

ERROR : java.lang.OutOfMemoryError java heap space

And

ERROR : java.lang.OutOfMemoryError: GC overhead limit exceeded

To resolve heap space issue I have added below config in spark-defaults.conf file. This works fine
spark.driver.memory 1g

In order to solve GC overhead limit exceeded issue I have added below config
spark.executor.memory 1g
spark.executor.extraJavaOptions Xmx1024m
spark.dirver.maxResultSize 2g

These configurations didn't work to 100% still i am facing same issue. Along which I am also getting

PSQL ERROR : org.postgresql.util.psqlexception ran out of memory retrieving query results

I am facing these issues while I am dealing with tables that have huge no.of rows i.e., news_mentions table has 4 540 092 records and size of table is 5 476 MB. SO it is taking even more time to execute spark-job which has to be done within seconds.
Here is my actual code.
from pyspark.sql import SparkSession
from pyspark import SparkContext
from pyspark.sql import SQLContext
from pyspark.sql import HiveContext

from pyspark.sql import DataFrameReader

sc = SparkContext()

sqlContext = SQLContext(sc)
sqlContext = HiveContext(sc)

input_media_source = " Times of India"

# Connecting to news_mentions table
df1 = sqlContext.read.format("jdbc").option("url", "jdbc:postgresql://localhost:5432/testdb").option("dbtable", "news_mentions").option("user", "postgres").load()
df1.createOrReplaceTempView("news_mentions")
news_mentions_DF = sqlContext.sql("SELECT * FROM news_mentions")

news_mentions_DF.show()

I'm facing GC limit exceeded error while performing show(). How to run my pyspark job quickly with high performance without any errors?
NOTE : I am running my pyspark job using spark-submit command without starting any standalone cluster mode.
My spark version - 2.2.0 with python version - 3.5.2