Re: Class, constructor and inherance.

2015-10-13 Thread Rikki Cattermole via Digitalmars-d-learn

On 13/10/15 5:17 PM, holo wrote:

On Tuesday, 13 October 2015 at 02:03:46 UTC, Rikki Cattermole wrote:

On 13/10/15 5:56 AM, holo wrote:

@Rikki:


If you didn't need to make it easily changeable I would say not even
bother with OOP at all.


Basically that what i had was enough for me and on top of that i could
build my app. It need to just periodically check for new instances and
if they are started or stopped and count "up and running time" for
billing purpose. But like i said i want to learn programming in D and
basically OOP too so i want to make it "proper way".

BTW: i think right now i understand what tuple is, but still don't know
for what to duplicate struct functionalities :). Those Templates still
don't understand but i hope that will came naturally with time and
practice. eg.. they are very similar to normal functions but you can
call it with types not only attributes.. strange ;)

I red yours advises and try to create according to it my own first
class.

I moved time functions and variables to method "go" they need to be
current as possible when im sending request, if wont authorization could
not pass.. so i think they shouldn't be in constructor.

I moved some other variables too, and created interface.

 From all that things came out such monster which is working and doing
its job :)

module sigawsv4;

import std.stdio, std.process;
import std.digest.sha, std.digest.hmac;
import std.string;
import std.conv;
import std.datetime;
import std.net.curl;

interface credential
{
 int go();
}

class sigv4 : credential
{
 //could be changed to take some structure as parameter instead of
such ammount of attributes

 this(string methodStr = "GET", string serviceStr = "ec2", string
hostStr = "ec2.amazonaws.com", string regionStr = "us-east-1", string
endpointStr = "https://ec2.amazonaws.com;, string payloadStr = "",
string parmStr = "Action=DescribeInstances=2013-10-15")
 {

 this.method = methodStr;
 this.service = serviceStr;
 this.host = hostStr;
 this.region = regionStr;
 this.endpoint = endpointStr;
 this.payload = payloadStr;
 this.request_parameters = parmStr;

 this.accessKey = environment.get("AWS_ACCESS_KEY");
 this.secretKey = environment.get("AWS_SECRET_KEY");
 }

 public:
 string method;
 string service;
 string host;
 string region;
 string endpoint;
 string payload;
 string request_parameters;


 int go()
 {
 //time need to be set when we are sending request not
before
 auto currentClock = Clock.currTime(UTC());
 auto currentDate = cast(Date)currentClock;
 auto curDateStr = currentDate.toISOString;
 auto currentTime = cast(TimeOfDay)currentClock;
 auto curTimeStr = currentTime.toISOString;
 auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z";

 canonicalURI = "/";
 canonicalQueryString = request_parameters;
 canonicalHeadersString =  "host:" ~ this.host ~ "\n" ~
"x-amz-date:" ~ xamztime ~ "\n";
 signedHeaders = "host;x-amz-date";

 auto canonicalRequest = getCanonicalRequest(canonicalURI,
canonicalQueryString, canonicalHeadersString, signedHeaders);

 string credentialScope = curDateStr ~ "/" ~ region ~ "/" ~
service ~ "/" ~ "aws4_request";

 string stringToSign = algorithm ~ "\n" ~ xamztime ~ "\n" ~
credentialScope ~ "\n" ~ sha256Of(canonicalRequest).toHexString.toLower;

 auto signingKey = getSignatureKey(secretKey, curDateStr,
region, service);

 string signature = hmac!SHA256(stringToSign.representation,
signingKey).toHexString.toLower;

 string authorizationHeader = algorithm ~ " " ~
"Credential=" ~ accessKey ~ "/" ~ credentialScope ~ ", " ~
"SignedHeaders=" ~ signedHeaders ~ ", " ~ "Signature=" ~ signature;


 auto client = HTTP(endpoint ~ "?" ~ canonicalQueryString);
 client.method = HTTP.Method.get;
 client.addRequestHeader("x-amz-date", xamztime);
 client.addRequestHeader("Authorization",
authorizationHeader);
 auto content = client.perform();

 return content;
 }

 private:
 const algorithm = "AWS4-HMAC-SHA256";

 string accessKey;
 string secretKey;

 string currentClock;
 string currentDate;
 string curDateStr;
 string currentTime;
 string curTimeStr;
 string xamztime;

 string canonicalURI;
 string canonicalQueryString;
string canonicalHeadersString;
string signedHeaders;



 alias sign = hmac!SHA256;

 auto getSignatureKey(string key, string dateStamp, string
regionName, string serviceName)
 {
 auto kString = ("AWS4" ~ key).representation;
 auto kDate = 

Re: OT: why do people use python when it is slow?

2015-10-13 Thread Ola Fosheim Grøstad via Digitalmars-d-learn

On Tuesday, 13 October 2015 at 23:26:14 UTC, Laeeth Isharc wrote:

https://www.quora.com/Why-is-Python-so-popular-despite-being-so-slow
Andrei suggested posting more widely.


That's flaimbait:

«Many really popular websites use Python. But why is that? 
Doesn't it affect the performance of the website?»


No. Really popular websites use pre-generated content / front end 
caches / CDNs or wait for network traffic from distributed 
databases.




Re: Regarding std.array.Appender

2015-10-13 Thread Suliman via Digitalmars-d-learn
On Monday, 5 March 2012 at 15:35:59 UTC, Steven Schveighoffer 
wrote:
On Wed, 29 Feb 2012 20:25:35 -0500, bearophile 
 wrote:


Do you know why std.array.Appender defines a "put" method 
instead of overloading the "~=" operator?


It should (in addition to put).  I see you have already filed 
an enhancement.


http://d.puremagic.com/issues/show_bug.cgi?id=4287

-Steve


Can I use Appender to add at end of every string my symbol?

foreach (pointsfile; pointsFiles)
{
File file = File(pointsfile, "r");
		string content = file.byLine; // I need to add "+" at at end of 
every string

}







Re: Regarding std.array.Appender

2015-10-13 Thread John Colvin via Digitalmars-d-learn

On Tuesday, 13 October 2015 at 13:21:54 UTC, Suliman wrote:
I tried to use map! but it's look like it do not work with 
string, becouse I got error: Error: no property 'map' for type 
'ByLine!(char, char)'


I suspect you don't have it imported.

import std.algorithm;

or

import std.algorithm : map;


Re: Regarding std.array.Appender

2015-10-13 Thread Suliman via Digitalmars-d-learn

On Tuesday, 13 October 2015 at 13:34:02 UTC, John Colvin wrote:

On Tuesday, 13 October 2015 at 13:21:54 UTC, Suliman wrote:
I tried to use map! but it's look like it do not work with 
string, becouse I got error: Error: no property 'map' for type 
'ByLine!(char, char)'


I suspect you don't have it imported.

import std.algorithm;

or

import std.algorithm : map;


Thanks, you are right! Can I add with map some element before and 
after string? map!(a=> a~=" +") work fine, but how to add before 
at same time?


Re: Regarding std.array.Appender

2015-10-13 Thread anonymous via Digitalmars-d-learn
On Tuesday 13 October 2015 15:47, Suliman wrote:

> something like: auto content = file.byLine.map!("start " ~ a=>a ~ 
> " end");

That's not how it works at all. Maybe stick to the examples of whatever 
resource you're learning from, for now.


Re: Regarding std.array.Appender

2015-10-13 Thread Suliman via Digitalmars-d-learn
I tried to use map! but it's look like it do not work with 
string, becouse I got error: Error: no property 'map' for type 
'ByLine!(char, char)'


Re: Regarding std.array.Appender

2015-10-13 Thread anonymous via Digitalmars-d-learn
On Tuesday 13 October 2015 15:42, Suliman wrote:

> map!(a=> a~=" +") work fine, but how to add before 
> at same time?

Use ~ instead of ~=, like so: map!(a => "+" ~ a ~ "+")


Re: Regarding std.array.Appender

2015-10-13 Thread Suliman via Digitalmars-d-learn
something like: auto content = file.byLine.map!("start " ~ a=>a ~ 
" end");


Re: Regarding std.array.Appender

2015-10-13 Thread Suliman via Digitalmars-d-learn

On Tuesday, 13 October 2015 at 13:55:07 UTC, anonymous wrote:

On Tuesday 13 October 2015 15:47, Suliman wrote:

something like: auto content = file.byLine.map!("start " ~ 
a=>a ~ " end");


That's not how it works at all. Maybe stick to the examples of 
whatever resource you're learning from, for now.


Yes, I understand that it's not work exactly like I try to use 
it, but which function can solve my problem?


Re: Regarding std.array.Appender

2015-10-13 Thread Suliman via Digitalmars-d-learn

On Tuesday, 13 October 2015 at 13:51:50 UTC, anonymous wrote:

On Tuesday 13 October 2015 15:42, Suliman wrote:

map!(a=> a~=" +") work fine, but how to add before at same 
time?


Use ~ instead of ~=, like so: map!(a => "+" ~ a ~ "+")


Thanks!


dynamic get from variantArray() data table

2015-10-13 Thread data pulverizer via Digitalmars-d-learn

Hi,

I am trying to use variantArray() as a data table object to hold 
columns each of which is an array of a specific type. I need to 
be able to get values from data table but I am having problems ...



import std.stdio; // i/o
import std.variant; // type variations

void main(){
  // Columns of the table
  string[] names = ["walter", "paul", "jeff", "andrie"];
  int[] age = [55, 62, 27, 52];
  string[] language = ["D", "Haskell", "Julia", "D"];
  Variant[] dt = variantArray(names, age, language);

  foreach(col; dt){
foreach(el; col){
  // here I try a kind of dynamic cast operator
  auto x = el.get!(type(el)); // gives error
  write(x);
}
write("\n");
  }
}

data_table.d(37): Error: cannot infer type for el
data_table.d(38): Error: undefined identifier 'el'

Help

DP



Re: dynamic get from variantArray() data table

2015-10-13 Thread Alex Parrill via Digitalmars-d-learn
On Tuesday, 13 October 2015 at 15:17:15 UTC, data pulverizer 
wrote:

Hi,

I am trying to use variantArray() as a data table object to 
hold columns each of which is an array of a specific type. I 
need to be able to get values from data table but I am having 
problems ...



import std.stdio; // i/o
import std.variant; // type variations

void main(){
  // Columns of the table
  string[] names = ["walter", "paul", "jeff", "andrie"];
  int[] age = [55, 62, 27, 52];
  string[] language = ["D", "Haskell", "Julia", "D"];
  Variant[] dt = variantArray(names, age, language);

  foreach(col; dt){
foreach(el; col){
  // here I try a kind of dynamic cast operator
  auto x = el.get!(type(el)); // gives error
  write(x);
}
write("\n");
  }
}

data_table.d(37): Error: cannot infer type for el
data_table.d(38): Error: undefined identifier 'el'

Help

DP


You're trying to iterate over a `Variant`, which isn't 
implemented.


You don't want to use a variant here anyway; you should use a 
struct or tuple for each entry in the table.




import std.typecons;

alias Entry = Tuple!(string, int, string);

void main() {
auto table = [Entry("walter", 55, "D"), Entry("paul", 62, 
"Haskell"), ... ]; // complete array

foreach(entry; table) {
writeln(entry.expand);
}
}



Re: Hash-Table-Based Multiple Arguments Replacement

2015-10-13 Thread Per Nordlöw via Digitalmars-d-learn

On Sunday, 11 October 2015 at 11:17:29 UTC, Nordlöw wrote:

Here's a solution:
https://github.com/nordlow/justd/blob/f8519e8a1af68bc25cc00c6ef12d13efa791250c/comparison_ex.d


Latest version contains a few fixes:

https://github.com/nordlow/justd/blob/master/comparison_ex.d


Re: Ternary if and ~ does not work quite well

2015-10-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, October 11, 2015 22:21:55 H. S. Teoh via Digitalmars-d-learn wrote:
> It's best to parenthesize when mixing other operators with ?, because ?
> has a pretty low precedence and may "steal" arguments from surrounding
> operators that you don't intend.  My suspicion is that what you wrote is
> being parsed as:
>
>   writeln(("foo " ~ true) ? "bar" : "baz");
>
> which is why you're getting unexpected output. Write instead:
>
>   writeln("foo " ~ (true ? "bar" : "baz"));
>
> If anything, it also helps readers of your code understand what it does
> without needing to consult the precedence table.

The ternary operator is on the next to bottom rung on the same level with
the various assigment operators. The _only_ operator with lower precedence
than the ternary operator is the comma (be it the comma operator or the
comma as an argument separator). So, if you're doing something like

auto i = foo == bar ? "hello" : "world";

or

foo(arg1, foo == bar ? "hello" : "world", arg3);

then the ternary is done before the stuff around it, but other than that,
everything around it is going to be done first. So, whether you need parens
are not is actually pretty straightforward. It pretty much boils down to if
you want the ternary to be done before anything else around it, use parens.
If you want the ternary operator to be done last, then you don't need them.

I actually think that the ternary operator is very easy to get right because
it's pretty much at one end of the operator precedence table rather than in
the middle where you have to remember what goes before and what comes after.
But for some reason, a lot of folks seem to assume that it has different
precedence than it has and have trouble with it.

- Jonathan M Davis



Re: dynamic get from variantArray() data table

2015-10-13 Thread Alex Parrill via Digitalmars-d-learn
On Tuesday, 13 October 2015 at 16:22:36 UTC, data pulverizer 
wrote:
Thanks for the suggestion Alex, however I need the dynamic 
behaviour properties of variantArray(), writing a struct each 
time would be undesirable.


Perhaps I could boil down the question to something like, is 
there a way of writing


auto x = dt[0][0];
auto y = x.get!(x.type - or whatever); // to get the actual 
value of x rather than .VariantN! ... type


For some kind of auto cast back to basic type.


The problem is that you can't do `x.get!(x.type)`, because the 
type of the expression must be known at compile time, but 
`x.type` is a runtime value. You'd have to create a branch for 
each type that `x.type` might be.


But again, unless you are dealing with a truly dynamic layout 
(and not a bunch of fixed layouts), there's better options. For 
example, you can use `std.range.zip` to iterate through each 
column array in lockstep, returning tuples for each element.



foreach(entry; zip(names, ages, languages)) {
write(entry.expand);
}




Re: dynamic get from variantArray() data table

2015-10-13 Thread data pulverizer via Digitalmars-d-learn
Thanks for the suggestion Alex, however I need the dynamic 
behaviour properties of variantArray(), writing a struct each 
time would be undesirable.


Perhaps I could boil down the question to something like, is 
there a way of writing


auto x = dt[0][0];
auto y = x.get!(x.type - or whatever); // to get the actual value 
of x rather than .VariantN! ... type


For some kind of auto cast back to basic type.

On Tuesday, 13 October 2015 at 15:51:40 UTC, Alex Parrill wrote:
On Tuesday, 13 October 2015 at 15:17:15 UTC, data pulverizer 
wrote:

Hi,

I am trying to use variantArray() as a data table object to 
hold columns each of which is an array of a specific type. I 
need to be able to get values from data table but I am having 
problems ...



import std.stdio; // i/o
import std.variant; // type variations

void main(){
  // Columns of the table
  string[] names = ["walter", "paul", "jeff", "andrie"];
  int[] age = [55, 62, 27, 52];
  string[] language = ["D", "Haskell", "Julia", "D"];
  Variant[] dt = variantArray(names, age, language);

  foreach(col; dt){
foreach(el; col){
  // here I try a kind of dynamic cast operator
  auto x = el.get!(type(el)); // gives error
  write(x);
}
write("\n");
  }
}

data_table.d(37): Error: cannot infer type for el
data_table.d(38): Error: undefined identifier 'el'

Help

DP


You're trying to iterate over a `Variant`, which isn't 
implemented.


You don't want to use a variant here anyway; you should use a 
struct or tuple for each entry in the table.




import std.typecons;

alias Entry = Tuple!(string, int, string);

void main() {
auto table = [Entry("walter", 55, "D"), Entry("paul", 62, 
"Haskell"), ... ]; // complete array

foreach(entry; table) {
writeln(entry.expand);
}
}





Struct toString works but not std.conv.to!string

2015-10-13 Thread Nordlöw via Digitalmars-d-learn

I have defined a struct UTCOffset in

https://github.com/nordlow/justd/blob/master/datetime_ex.d

Everything works as desired except for

import std.conv : to;
assert(UTCOffset(+14, 0).to!string == "UTC+14:00");

which fails as

/usr/include/dmd/phobos/std/conv.d(293,14): Error: template 
instance isRawStaticArray!() does not match template declaration 
isRawStaticArray(T, A...)
datetime_ex.d(129,29): Error: cannot resolve type for 
UTCOffset(cast(ubyte)0u).this(cast(byte)14, 
cast(ubyte)0u).to!string


I don't understand what's wrong.


Re: Struct toString works but not std.conv.to!string

2015-10-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, October 13, 2015 21:07:07 Nordlöw via Digitalmars-d-learn wrote:
> I have defined a struct UTCOffset in
>
> https://github.com/nordlow/justd/blob/master/datetime_ex.d
>
> Everything works as desired except for
>
>  import std.conv : to;
>  assert(UTCOffset(+14, 0).to!string == "UTC+14:00");
>
> which fails as
>
> /usr/include/dmd/phobos/std/conv.d(293,14): Error: template
> instance isRawStaticArray!() does not match template declaration
> isRawStaticArray(T, A...)
> datetime_ex.d(129,29): Error: cannot resolve type for
> UTCOffset(cast(ubyte)0u).this(cast(byte)14,
> cast(ubyte)0u).to!string
>
> I don't understand what's wrong.

Just glancing at your code, you've marked toString with @property, which is
kind of a weird thing to do, nd if we ever make @property enforce that it's
not called with parens, then that code won't work. So, you might try moving
that @property: to after toString and see if that fixes your problem. But
given the error, my guess is that the problem relates to the fact that you
templatized the constructor, which is often problematic, and whatever type
introspection std.conv.to is doing could be choking on that. So, you should
probably try making it so that the constructor isn't templatized and see if
that fixes the problem.

- Jonathan M Davis




Re: Struct toString works but not std.conv.to!string

2015-10-13 Thread Ali Çehreli via Digitalmars-d-learn

On 10/13/2015 02:07 PM, Nordlöw wrote:

I have defined a struct UTCOffset in

https://github.com/nordlow/justd/blob/master/datetime_ex.d

Everything works as desired except for

 import std.conv : to;
 assert(UTCOffset(+14, 0).to!string == "UTC+14:00");

which fails as

/usr/include/dmd/phobos/std/conv.d(293,14): Error: template instance
isRawStaticArray!() does not match template declaration
isRawStaticArray(T, A...)
datetime_ex.d(129,29): Error: cannot resolve type for
UTCOffset(cast(ubyte)0u).this(cast(byte)14, cast(ubyte)0u).to!string

I don't understand what's wrong.


Reduced with a workaround:

struct UTCOffset
{
import std.conv : to;// Move to module scope to compile

string toString() const
{
return "hello";
}
}

void main() {
import std.conv : to;
UTCOffset().to!string;
}

This is an issue that I know to be known. :) I think it is about private 
definitions (isRawStaticArray) of modules not working outside? or when 
in inner scopes? Something like that...


Ali



Re: Struct toString works but not std.conv.to!string

2015-10-13 Thread anonymous via Digitalmars-d-learn

On Tuesday, 13 October 2015 at 22:21:43 UTC, Ali Çehreli wrote:

Reduced with a workaround:

struct UTCOffset
{
import std.conv : to;// Move to module scope to compile


This introduces UTCOffset.to as an alias to std.conv.to.


string toString() const
{
return "hello";
}
}

void main() {
import std.conv : to;


This ends up being ignored, because UTCOffset has a member called 
`to`.



UTCOffset().to!string;


This does not do call std.conv.to through UFCS. Instead, it calls 
UTCOffset's static alias of std.conv.to without an argument.


That is: `UTCOffset().to!string;` = `UTCOffset.to!string;` = 
`std.conv.to!string;`



}




Re: Struct toString works but not std.conv.to!string

2015-10-13 Thread Nordlöw via Digitalmars-d-learn
On Tuesday, 13 October 2015 at 21:50:54 UTC, Jonathan M Davis 
wrote:
Just glancing at your code, you've marked toString with 
@property, which is kind of a weird thing to do, nd if we ever 
make @property enforce that it's not called with parens, then 
that code won't work. So, you might try moving that @property: 
to after toString and see if that fixes your problem. But given 
the error, my guess is that the problem relates to the fact 
that you templatized the constructor, which is often 
problematic, and whatever type introspection std.conv.to is 
doing could be choking on that. So, you should probably try 
making it so that the constructor isn't templatized and see if 
that fixes the problem.


- Jonathan M Davis


None of you advice helped.

Fortunately I found a solution:

If I move

import std.conv : to;

into the function scopes

the problem goes away.

Thanks, anyway, for you time.


OT: why do people use python when it is slow?

2015-10-13 Thread Laeeth Isharc via Digitalmars-d-learn

https://www.quora.com/Why-is-Python-so-popular-despite-being-so-slow
Andrei suggested posting more widely.