Tuesday, November 26, 2019

Sated RTD test

Friday, August 31, 2018

A new PyQt post: Retaining widget shape

Just for fun I'm working on a Tetris game. I based this on the code originally published by Jan Bodnar, cleaning it up and commenting the living bejeezus out of it.

My code is here on Github. The file standard.py is playable, but it has one big flaw. If you start it up and then drag on the corner of the window, the tetris pieces become misshapen, distorted.

So how can I make (Py)Qt5 retain the aspect ratio of a widget while still allowing the user to stretch or shrink its container?

Not surprisingly, I am not the first to wonder this. (It would come up when coding any kind of game, I should think.) Search the Qt General Forum on the string "heightForWidth" (which is the method name that turns up in most answers to this issue) and you'll find several postings from as long as seven years ago, and as recently as one year. A more general search turns up Stack Overflow posts. Almost all the proposed solutions are wrong. However, the solution offered in this S.O. post has code that works.

Most of the solutions say that to make a widget keep its aspect ratio, you

  • Give it an explicit SizePolicy in which
  • you call setHeightForWidth(True) and
  • in the widget itself, override the hasHeightForWidth() and heightForWidth() methods:
    def hasHeightForWidth(self):
        return True
    def heightForWidth(self, width):
        return width

Except some posts say the widget has to be in a layout (e.g. QVBoxLayout), and others seem to say that you have to implement a custom version of a box layout yourself. I've got a simple test case right here in which I implemented all those things in every combination. You're welcome to play with it.

Bottom line is, no heightForWidth() method is ever called, that I could find. The whole approach probably works for some combination of options—it certainly appears to have been devised to do this—but it flat doesn't work in any combination that I could devise.

Like I said, download that test code and see if you can find the magic.

What does work is this: in the widget class that you want to always be square, implement the resizeEvent() handler. In it, check the new size of the widget and adjust its contentsMargins appropriately to compensate. Thus:

    def resizeEvent(self, event):
        # setContentsMargins(left,top,right,bottom)
        d = self.width()-self.height()
        if d : # is not zero,
            mod1 = abs(d)//2
            mod2 = abs(d)-mod1
            if d > 0 : # width is greater, reduce it
                self.setContentsMargins(mod1,0,mod2,0)
            else : # height is greater, reduce it
                self.setContentsMargins(0,mod1,0,mod2)
        super().resizeEvent(event)

The same code, with a little more math, could be used to maintain some other aspect ratio than a square.

Saturday, August 6, 2016

A Close Reading of Heinlein's The Menace from Earth (part 9)

this essay begins in part 1

Who loves ya, baby?

Holly wakes in a hospital room with casts on both arms. There is pleasant banter with a nurse and then a doctor. She asks after Ariel.

"She's right here," Ariel agreed from the door. "May I come in?"

Ariel is on crutches. Actually, he doesn't say crutches, he only says she "hopped" into the room. But it turns out she has cracked ribs. If you have cracked ribs, you don't "hop", or if you do, you scream.

"You hurt your foot."

She shrugged. "Nothing. A sprain and a torn ligament. Two cracked ribs. But I would have been dead. You know why I'm not?"

I didn't answer. She touched one of my casts. "That's why. You broke my fall and I landed on top of you. You saved my life and I broke your arms."

… I didn't have an answer so I said, "Where's Jeff? Is he all right?"

"He'll be along soon. Jeff's not hurt . . . though I'm surprised he didn't break both ankles. He stalled in beside us so hard he should have. But Holly . . . Holly my very dear . . . I slipped in so that you and I could talk about him before he got here."

Holly diverts the conversation; it seems Ariel is going back to earth soon. Ariel returns to the topic on her mind.

" … But Holly . . . listen please and don't get angry. Its about Jeff. He hasn't treated you very well the last few days … But don't be angry with him. I'm leaving and everything will be the same."

Holly gets on her high horse and explains how she's a career woman and doesn't need romance. In quite a lengthy give-and-take (longer than necessary, I think) Ariel tries to straighten out her prideful confusion, finally ending with

… "How old am I?"

I managed not to boggle. "Huh? Older than Jeff thinks you are. Twenty-one at least. Maybe twenty-two."

She sighed. "Holly, I'm old enough to be your mother."

Holly doesn't believe it. (Quickly doing arithmetic: Holly's fifteen, Ariel could be in her thirties? Yeah, a 33-year-old actress in good shape; it checks out.)

"But that's why, though Jeff is a dear, there never was a chance that I could fall in love with him. But … the important thing is that he loves you."

"What? That's the silliest thing you've said yet! Oh, he likes me -- or did. But that's all." …

"Wait, Holly. I saw something you didn't because you were knocked cold. When you and I bumped, do you know what happened?"

"Uh, no."

"Jeff arrived like an avenging angel a split second behind us. He was ripping his wings off as he hit, getting his arms free. He didn't even look at me. He just stepped across me and picked you up and cradled you in his arms, all the while bawling his eyes out."

"He did?"

Here there is more self-conscious mulling by Holly, but she still refuses to break out of her "just partners" mindset. Then Jeff comes in.

He stopped in the door and looked at us, frowning.

"Hi, Ariel."

"Hi, Jeff."

"Hi, Fraction." He looked me over. "My, but you're a mess."

"You aren't very pretty yourself. I hear you have flat feet."

"Permanently. How do you brush your teeth with those things on your arms?"

"I don't."

Ariel leaves and Jeff says "Hold still" and kisses her.

… I was startled speechless because Jeff never kisses me, except birthday kisses, which don't count. But I tried to kiss back to show that I appreciated it.

… "Runt," he said mournfully, "you sure give me a lot of grief."

"You're no bargain yourself, flathead," I answered with dignity.

"I suppose not." He looked me over sadly. "What are you crying for?"

I didn't know that I had been. Then I remembered why. "Oh, Jeff -- I busted my pretty wings!"

"We'll get you some more. Uh, brace yourself. I'm going to do it again."

"All right." He did.

I supposed Hardesty & Hardesty has more rhythm than Jones & Hardesty.

It really sounds better.

OK, I'm just an old softy and I still get sniffly from "He just stepped across me and picked you up and cradled you in his arms", right on to the end. It's so sweet!

That said, there is a lot that could be better in this final scene. It's too long and talky. More seriously, Ariel and Jeff seem to have come out of the disaster with some new, deeper self-knowledge and changed attitudes. Holly has not. Right to the end she seems stuck in the obsession with Jeff as a partner in their design firm, unable to recognize a new, closer relation. You could say the last two lines show that change of attitude, but I'd like something more explicit -- something to show clearly she's stepped out of being a nerdy kid.

And in conclusion,

It was only on this careful re-reading that I realized that the argument where Ariel wants to try real wings and Jeff butts into the discussion is the pivot of the story. The cross-current of mistakes and different kinds of hurt pride (Jeff's, Holly's and also Ariel's) creates the final situation. All of Jeff's infatuation and Holly's hurt and jealousy feed into it, along with Ariel's basic selfishness. Out the other side comes a sequence of bad decisions that almost ends in tragedy but instead ends by crystallizing everyone's feelings. Despite my many nit-pickings, it's a finely-structured story.

They don't make science fiction feature movies without explosions in them, so this will never be a movie (or if it is, it will be unrecognizable). But if the SyFy channel wanted to make a nice special, this would do beautifully. Look at at the practicality: it has a small cast; the sets are all interiors; there's not a lot of special effects. The flying could all be done on wires in front of green-screens. And it's really a good story.

A Close Reading of Heinlein's The Menace from Earth (part 8)

this essay begins in part 1

Older Woman Wisdom?

They fuss about looking over second-hand wing sets and find one that fits.

While I was helping her into the tail surfaces I said, "Ariel? This is still a bad idea."

"I know. But we can't let men think they own us."

"I suppose not."

"They do own us, of course. But we shouldn't let them know it."

I really don't want to bash RAH for sexism even more. But putting "They do own us, of course" in the mouth of his "wise older woman" character... really? It's a kind of pop-folk pseudo-wisdom that you might find in an Ozzie and Harriet script. It was somewhat true in Victorian England, but by the 1950's women had the vote, could own property, had created "Rosie the Riveter" as a national meme. And it certainly did not have to be the assumptive truth of a future Lunar society. But Heinlein, apparently, assumed it was basic to human nature.

Disaster

Anyway, Holly completes fitting Ariel out with her second-hand wings.

"All right. Wups! I goofed. They aren't orange."

"Does it matter?"

"It sure does."

With the wings painted beginner-orange, Ariel tries them out on the beginner slope. After some time she begins to eye the central updraft, the Baby's ladder. Holly's not sure, and cautions her at length. They start circling up.

"Not tired?"

"Heavens, no! Girl, I'm living!" She giggled. "And mama said I'd never be an angel!"

I didn't answer because red-and-silver wings came charging at me, braked suddenly and settled into the circle between me and Ariel. Jeff's face was almost as red as his wings. "What the devil do you think you are doing?"

"Orange wings!" I yelled. "Keep clear!"

"Get down out of here! Both of you!"

… "Jeff Hardesty," I said savagely, "I give you three seconds to get out from between us -- then I'm going to report you for violation of Rule One. For the third time -- Orange Wings!"

Jeff moves off although stays near; the women continue the slow circling climb, Holly fretting that Ariel might be getting tired.

… "Ariel? Tired now?"

"No."

"Well, I am. Could we go down, please?"

She didn't argue, she just said "All right. What am I to do?"

"Lean right and get out of the circle." I intended to have her move out five or six hundred feet, get into the return down draft, and circle the cave down instead of up. I glanced up, looking for Jeff. … I glanced back at Ariel.

I couldn't find her.

Then I saw her, a hundred feet below -- flailing her wings and falling out of control.

… I was simply filled with horror. I seemed to hang there frozen for an hour while I watched her.

But the fact appears to be that I screamed "Jeff!" and broke into a stoop.

But I didn't seem to fall, coudn't overtake her. I spilled my wings completely -- but couldn't manage to fall; she was as far away as ever. … I could feel rushing air -- but I still didn't seem to close on her. … This nightmare dragged on for hours.

Actually we didn't have room to fall for more than twenty seconds; that's all it takes to stoop a thousand feet. But twenty seconds can be horribly long . . . long enough to regret every foolish thing I had ever done or said, long enough to say a prayer for both of us . . . and to say good-by to Jeff in my heart.

… and I was overtaking her . . . I was passing her -- I was under her!

Then I was braking with everything I had, almost pulling my wings off. I grabbed air, held it, and started to beat without ever going to level flight. I beat once, twice, three times . . . and hit her from below, jarring us both.

Then the floor hit us.


Wow.

continue in part 9

Friday, August 5, 2016

A Close Reading of Heinlein's The Menace from Earth (part 7)

this essay begins in part 1

Teaching Ariel to fly

So I taught Ariel Brentwood to "fly." Look, those so-called wings they let tourists wear have fifty square feet of lift surface, no controls except a warp in the primaries, a built-in dihedral to make them stable as a table … The tail is rigid, and canted so that if you stall (almost impossible) you land on your feet. …

I put myself to the humiliation of strapping on a set of the silly things and had Ariel watch while I swung into the Baby's Ladder and let it carry me up a hundred feet to show her that you really and truly could "fly" with them. Then I thankfully got rid of them, strapped her into a larger set, and put on my beautiful Storer-Gulls. I had chased Jeff away … but when he saw her wing up, he swooped down and landed by us.

I looked up. "You again."

"Hello, Ariel. Hi, Blip. Say, you've got her shoulder straps too tight."

"Tut, tut," I said. "One coach at a time, remember? … get above two hundred feet and stay there, we don't need any dining-lounge pilots."

Jeff pouted like a brat but Ariel backed me up. "Do what teacher says, Jeff, that's a good boy."

With Jeff circling above, Holly gets on with the teaching.

I admit Ariel was a good pupil … I found myself almost liking her as long as I kept my mind firmly on teaching. She tried hard and learned fast … she admitted diffidently that she had had ballet training.

Overdramatic metaphor

About mid-afternoon she said, "Could I possibly try real wings?"

Holly tries to talk her out of it.

"… You might get hurt, even killed."

"Would you be held responsible?"

"No, you signed a release when you came in."

"Then I'd like to try it."

… to let her do something too dangerous while she was my pupil . . . well, it smacked of David and Uriah.

It seems Holly knows her Bible; but my goodness that is a dramatic reference. The story is in 2 Samuel 11: David, king of Israel, has a thing going with Bathsheba, the wife of one of his officers, Uriah. When Uriah's presence becomes inconvenient, David sends him to war, carrying a dispatch to to the general saying, "Set Uriah in the forefront of the hottest battle, and retreat from him, that he may be struck down and die."

On reflection, I think it is not over-dramatic. It underscores Holly's serious concern that she could be putting Ariel in real danger, and that doing so might be mis-perceived as malice. And it adds weight to what is about to happen.

"Ariel, I can't stop you . . . but I should put my wings away and not have anything to do with it."

"If you feel that way, I can't ask you to coach me. … Perhaps Jeff will help me."

"He probably will," I blurted out, "if he is as big a fool as I think he is!"

The critical moment

What follows is the emotional crux of the story, where a wrong path is taken owing to a complex mix of misunderstandings, anger, and pride. Everything in the story leads up to this; and the conclusion inevitably follows from it.

Her company face slipped but she didn't say anything because just then Jeff stalled beside us. "What's the discussion?"

We both tried to tell him and confused him for he got the idea I had suggested it, and started bawling me out. Was I crazy? Was I trying to get Ariel hurt? Didn't I have any sense?

"Shut up!" I yelled, then added quietly but firmly, "Jefferson Hardesty, you wanted me to teach your girl friend, so I agreed. But don't butt in and don't think you can get away with talking to me like that. Now beat it! Take wing. Grab air!"

He swelled up and said slowly, "I absolutely forbid it."

Silence for five long counts. Then Ariel said quietly, "Come, Holly. Let's get me some wings."

"Right, Ariel."

continue in part 8

Thursday, August 4, 2016

A Close Reading of Heinlein's The Menace from Earth (part 6)

this essay begins in part 1

We finally go flying

We finally get to the signature image of this story, the one that has made it stick in so many people's memories for years: the vision of being able to fly like a bird. This activity is so different from normal experience that you might think it would require a lot of exposition to set up and justify. Nope. There was one prior indirect reference to Lunar gravity. Now he only adds,

Most of the stuff written about the Bats' Cave gives a wrong impression. It's the air storage tank for the city, just like all the colonies have … We just happen to be lucky enough to have one big enough to fly in. But it never was built, or anything like that; it's just a big volcanic bubble, two miles across, and if it had broken through, way back when, it would have been a crater.

There was once some theorizing that Lunar craters were the result of vulcanism. Now they are all assumed to be from impacts, but you know? For the sake of this story, we can believe there are volcanic "bubbles" under the surface. We don't know there aren't.

Now Holly nerds out over her wings. This is a lovely demonstration of a science fiction technique: if you let a character wax passionate about some feature you need to explain, you build the character and simultaneously get painless, even entertaining, exposition.

I left my shoes and skirt in the locker room and slipped my tail surfaces on my feet, then zipped into my wings and got someone to tighten the shoulder straps. My wings aren't ready-made condors; they are Storer-Gulls, custom-made for my weight distribution and dimensions. I've cost Daddy a pretty penny in wings, outgrowing them so often, but these latest I bought myself with guide fees.

They're lovely! -- titanalloy struts as light and strong as bird bones, tension-compensated wrist-pinion and shoulder joints, natural action in the alula slots, and automatic flap action in stalling. The wing skeleton is dressed in styrene feather-foils with individual quilling of scapulars and primaries. They almost fly themselves.

After that outburst you are ready to believe this is credible. You can check the words, as Heinlein no doubt did:, "alula: the group of three to six small, rather stiff feathers growing on the first digit, or thumb of a bird's wing." Scapulars are short feathers on the shoulder, and primaries are the big feathers along the edge of the wing.

Holly cycles through an air lock into the cave proper.

… I perked up and felt sorry for all groundhogs, tied down by six times proper weight, who never, never, never could fly.

Not even I could, on Earth. My wing loading is less than a pound per square foot, as wings and all I weigh less than twenty pounds. … I spread my wings, ran a few steps, warped for lift and grabbed air -- lifted my feet and was airborne.

Now follows six longish paragraphs of Holly describing flying and various techniques of flight: soaring, gliding, and "stooping like a hawk". She checks the sightseer's gallery for "Jeff and his groundhogess"; then meets a friend, Mary, in mid-air and they agree to perch to talk.

Mary has gossip: Jeff is teaching "that Earthside siren" to fly right now. She goads Holly maliciously, insisting that everybody knows she is "simply simmering with jealousy".

I watched her out of sight, then sneaked my left hand out of the hand slit and got at my hanky -- awkward when you are wearing wings but the floodlights had made my eyes water. … Then I reminded myself that I had been planning to be a spaceship designer like Daddy long before Jeff and I teamed up. I wasn't dependent on anyone; I could stand alone, like Joan of Arc, or Lise Meitner.

It is really interesting that Heinlein chose the name of Lise Meitner as hero for Holly. Meitner, co-discoverer with Otto Hahn of nuclear fission, fled Nazi Germany and spent the war years in Sweden. She lectured in the U.S. in the 1950s and received many official honors around that time, but is little-known today.

An awkward conversation

Jeff flies up.

He landed by me but didn't sidle up. "Hi, Decimal Point."

"Hi, Zero. Uh, stolen much lately?"

"Just the City Bank but they made me put it back." He frowned and added, "Holly, are you mad at me?"

… "Of course not. Why should I be?

… "Uh, that's fine. Look, Test Sample, do me a favor. Help me out with a friend -- a client that is -- well, she's a friend, too. She wants to learn to use glide wings."

So, Jeff actually wants them to get to know each other. Holly would rather eat glass but,

… what I did say was, "OK, Jeff," then gathered the fox to my bosom and dropped off into a glide.

It took a while to chase down the reference to the fox in her bosom. Holly has apparently read Plutarch's Lives of the noble Grecians and Romans. Describing the upbringing of Spartan children, he says "So seriously did the Lacedaemonian children go about their stealing, that a youth, having stolen a young fox and hid it under his coat, suffered it to tear out his very bowels with its teeth and claws, and died upon the place, rather than let it be seen."

So the expression "fox in my bosom" means heroically letting a guilty secret eat your guts rather than admitting to it.

continue in part 7

Wednesday, August 3, 2016

A Close Reading of Heinlein's The Menace from Earth (part 5)

This essay begins in part 1

Jeff is smitten

Now things start to go south for Holly.

Jeff's eyes widened and I felt uneasy … I knew she was exceptionally decorative, but it was unthinkable that Jeff could be captivated by any groundhog, no matter how well designed. They don't speak our language!

I am not romantic about Jeff; we are simply partners. But anything that affects Jones & Hardesty affects me.

When we joined him at West Lock he almost stepped on his tongue in a disgusting display of adolescent rut. I was ashamed of him and, for the first time, apprehensive. Why are males so childish?

This all seems right to me. As long as the story is focused on people, it works. It's only the auxiliary hardware that causes problems.

Holly helps Miss Brentwood get into a pressure suit.

Those rental suits take careful adjusting or they will pinch you in tender places once out in vacuum . . . besides there are things about them that one girl ought to explain to another.

Jeff and "the platinum menace" go out without asking Holly to come along.

The days that followed were the longest in my life. I saw Jeff only once . . . on the slidebelt in Diana Boulevard, going the other way. She was with him.

She knows (by some unspecified means) that Jeff is cutting classes and taking Miss Brentwood to night clubs.

Starship Prometheus fading...

At last we get a good look at Holly's obsession, the starship that she and Jeff are designing in their spare time.

Jones & Hardesty had a tremendous backlog because we were designing Starship Prometheus. This project we had been slaving over for a year, flying not more than twice a week to devote time to it -- and that's a sacrifice.

Again with the "flying". Wonder what that means?

Of course you can't build a starship today, because of the power plant. But Daddy thinks that there will soon be a technological break-through and mass-conversion power plants will be built -- which means starships. …
Jones & Hardesty plans to be ready with a finished proposal while other designers are still floundering … We had been working every possible chance … checking each other's computations, fighting bitterly over details, and having a wonderful time. But the very day I introduced him to Ariel Brentwood, he failed to appear.

Days go by; no Jeff. Holly tries to come to terms with it.

I looked at the name plate of the sheet I was revising. "Jones & Hardesty" it read, like all the rest. I said to myself, "Holly Jones, quit bluffing; this may be The End. You knew someday Jeff would fall for somebody." … I erased "Jones & Hardesty and lettered "Jones & Company" and stared at it. Then I started to erase that, too -- but it smeared; I had dripped a tear on it. Which was ridiculous!

Awwww! This is such a great moment; I love it.

Too bad I have to nit-pick it. Dear millenial reader: once upon a time, engineers drew their designs by hand on big sheets of paper. Every official design sheet had a signature block, a box, usually in the lower left corner, with the name of the company, the date, revision number and so forth. This, like all the text on the sheet, was hand-lettered. So picture Holly bent over a table piled with many big sheets of paper, each a section of the design of the Prometheus, and each sheet with its neat name plate hand-lettered in the lower corner. That's what we're talking about here.

If we rewrote the story for this century, she'd be working on a table-top display screen maybe. But the falling tear wouldn't be as effective.

Holly's parents are concerned; she's been "moping" and not eating much, her father says. With their encouragement she decides to go flying. She mopes her way to the Bat Cave, fighting her own emotions.

… Jeff had been my partner and pal, and under my guidance he could have become a great spaceship designer, but our relationship was straightforward . . . a mutual respect for each other's abilities, with never any of that lovey-dovey stuff. …

No, I couldn't be jealous; I was simply worried sick because my partner had become involved with a groundhog.

continue in part 6